blob: 9b8c2ddd06359fd2506f9eb4bba41522f6f2a004 [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
44 isI64IntS10Immediate(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
49 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
50 bool
51 isI32IntS10Immediate(ConstantSDNode *CN)
52 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000053 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000054 }
55
Scott Michel504c3692007-12-17 22:32:34 +000056 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
57 bool
58 isI32IntU10Immediate(ConstantSDNode *CN)
59 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000060 return isUInt<10>(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000061 }
62
Scott Michel266bc8f2007-12-04 22:23:35 +000063 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
64 bool
65 isI16IntS10Immediate(ConstantSDNode *CN)
66 {
Benjamin Kramer7e09deb2010-03-29 19:07:58 +000067 return isInt<10>(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000068 }
69
70 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
71 bool
72 isI16IntS10Immediate(SDNode *N)
73 {
Scott Michel9de57a92009-01-26 22:33:37 +000074 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
75 return (CN != 0 && isI16IntS10Immediate(CN));
Scott Michel266bc8f2007-12-04 22:23:35 +000076 }
77
Scott Michelec2a08f2007-12-15 00:38:50 +000078 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
79 bool
80 isI16IntU10Immediate(ConstantSDNode *CN)
81 {
Benjamin Kramer34247a02010-03-29 21:13:41 +000082 return isUInt<10>((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000083 }
84
85 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
86 bool
87 isI16IntU10Immediate(SDNode *N)
88 {
89 return (N->getOpcode() == ISD::Constant
90 && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
91 }
92
Scott Michel266bc8f2007-12-04 22:23:35 +000093 //! ConstantSDNode predicate for signed 16-bit values
94 /*!
95 \arg CN The constant SelectionDAG node holding the value
96 \arg Imm The returned 16-bit value, if returning true
97
98 This predicate tests the value in \a CN to see whether it can be
99 represented as a 16-bit, sign-extended quantity. Returns true if
100 this is the case.
101 */
102 bool
103 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
104 {
Owen Andersone50ed302009-08-10 22:56:29 +0000105 EVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000106 Imm = (short) CN->getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +0000107 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000108 return true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000109 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000110 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000111 short s_val = (short) i_val;
112 return i_val == s_val;
113 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000114 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000115 short s_val = (short) i_val;
116 return i_val == s_val;
117 }
118
119 return false;
120 }
121
122 //! SDNode predicate for signed 16-bit values.
123 bool
124 isIntS16Immediate(SDNode *N, short &Imm)
125 {
126 return (N->getOpcode() == ISD::Constant
127 && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
128 }
129
130 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
131 static bool
132 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
133 {
Owen Andersone50ed302009-08-10 22:56:29 +0000134 EVT vt = FPN->getValueType(0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000136 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000137 int sval = (int) ((val << 16) >> 16);
138 Imm = (short) val;
139 return val == sval;
140 }
141
142 return false;
143 }
144
Scott Michel053c1da2008-01-29 02:16:57 +0000145 bool
Scott Michel02d711b2008-12-30 23:28:25 +0000146 isHighLow(const SDValue &Op)
Scott Michel053c1da2008-01-29 02:16:57 +0000147 {
148 return (Op.getOpcode() == SPUISD::IndirectAddr
149 && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
150 && Op.getOperand(1).getOpcode() == SPUISD::Lo)
151 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
152 && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
153 }
154
Scott Michel266bc8f2007-12-04 22:23:35 +0000155 //===------------------------------------------------------------------===//
Owen Andersone50ed302009-08-10 22:56:29 +0000156 //! EVT to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000157
158 struct valtype_map_s {
Owen Andersone50ed302009-08-10 22:56:29 +0000159 EVT VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000160 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000161 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michelf0569be2008-12-27 04:51:36 +0000162 unsigned lrinst; /// LR instruction
Scott Michel266bc8f2007-12-04 22:23:35 +0000163 };
164
165 const valtype_map_s valtype_map[] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000166 { MVT::i8, SPU::ORBIr8, true, SPU::LRr8 },
167 { MVT::i16, SPU::ORHIr16, true, SPU::LRr16 },
168 { MVT::i32, SPU::ORIr32, true, SPU::LRr32 },
169 { MVT::i64, SPU::ORr64, false, SPU::LRr64 },
170 { MVT::f32, SPU::ORf32, false, SPU::LRf32 },
171 { MVT::f64, SPU::ORf64, false, SPU::LRf64 },
Scott Michel58c58182008-01-17 20:38:41 +0000172 // vector types... (sigh!)
Owen Anderson825b72b2009-08-11 20:47:22 +0000173 { MVT::v16i8, 0, false, SPU::LRv16i8 },
174 { MVT::v8i16, 0, false, SPU::LRv8i16 },
175 { MVT::v4i32, 0, false, SPU::LRv4i32 },
176 { MVT::v2i64, 0, false, SPU::LRv2i64 },
177 { MVT::v4f32, 0, false, SPU::LRv4f32 },
178 { MVT::v2f64, 0, false, SPU::LRv2f64 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000179 };
180
181 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
182
Owen Andersone50ed302009-08-10 22:56:29 +0000183 const valtype_map_s *getValueTypeMapEntry(EVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000184 {
185 const valtype_map_s *retval = 0;
186 for (size_t i = 0; i < n_valtype_map; ++i) {
187 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000188 retval = valtype_map + i;
189 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000190 }
191 }
192
193
194#ifndef NDEBUG
195 if (retval == 0) {
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000196 report_fatal_error("SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns"
197 "NULL for " + Twine(VT.getEVTString()));
Scott Michel266bc8f2007-12-04 22:23:35 +0000198 }
199#endif
200
201 return retval;
202 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000203
Scott Michel7ea02ff2009-03-17 01:15:45 +0000204 //! Generate the carry-generate shuffle mask.
205 SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
206 SmallVector<SDValue, 16 > ShufBytes;
Dan Gohman844731a2008-05-13 00:00:25 +0000207
Scott Michel7ea02ff2009-03-17 01:15:45 +0000208 // Create the shuffle mask for "rotating" the borrow up one register slot
209 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
211 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
212 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
213 ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
Scott Michel266bc8f2007-12-04 22:23:35 +0000214
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000216 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000217 }
Scott Michel02d711b2008-12-30 23:28:25 +0000218
Scott Michel7ea02ff2009-03-17 01:15:45 +0000219 //! Generate the borrow-generate shuffle mask
220 SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
221 SmallVector<SDValue, 16 > ShufBytes;
222
223 // Create the shuffle mask for "rotating" the borrow up one register slot
224 // once the borrow is generated.
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
226 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
227 ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
228 ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000229
Owen Anderson825b72b2009-08-11 20:47:22 +0000230 return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000231 &ShufBytes[0], ShufBytes.size());
Scott Michel266bc8f2007-12-04 22:23:35 +0000232 }
233
Scott Michel7ea02ff2009-03-17 01:15:45 +0000234 //===------------------------------------------------------------------===//
235 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
236 /// instructions for SelectionDAG operations.
237 ///
238 class SPUDAGToDAGISel :
239 public SelectionDAGISel
240 {
Dan Gohmand858e902010-04-17 15:26:15 +0000241 const SPUTargetMachine &TM;
242 const SPUTargetLowering &SPUtli;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000243 unsigned GlobalBaseReg;
Scott Michel02d711b2008-12-30 23:28:25 +0000244
Scott Michel7ea02ff2009-03-17 01:15:45 +0000245 public:
246 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
247 SelectionDAGISel(tm),
248 TM(tm),
249 SPUtli(*tm.getTargetLowering())
250 { }
251
Dan Gohmanad2afc22009-07-31 18:16:33 +0000252 virtual bool runOnMachineFunction(MachineFunction &MF) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000253 // Make sure we re-emit a set of the global base reg if necessary
254 GlobalBaseReg = 0;
Dan Gohmanad2afc22009-07-31 18:16:33 +0000255 SelectionDAGISel::runOnMachineFunction(MF);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000256 return true;
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000257 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000258
Scott Michel7ea02ff2009-03-17 01:15:45 +0000259 /// getI32Imm - Return a target constant with the specified value, of type
260 /// i32.
261 inline SDValue getI32Imm(uint32_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000262 return CurDAG->getTargetConstant(Imm, MVT::i32);
Scott Michel94bd57e2009-01-15 04:41:47 +0000263 }
264
Scott Michel7ea02ff2009-03-17 01:15:45 +0000265 /// getI64Imm - Return a target constant with the specified value, of type
266 /// i64.
267 inline SDValue getI64Imm(uint64_t Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 return CurDAG->getTargetConstant(Imm, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000269 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000270
Scott Michel7ea02ff2009-03-17 01:15:45 +0000271 /// getSmallIPtrImm - Return a target constant of pointer type.
272 inline SDValue getSmallIPtrImm(unsigned Imm) {
273 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Scott Michel266bc8f2007-12-04 22:23:35 +0000274 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000275
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000276 SDNode *emitBuildVector(SDNode *bvNode) {
277 EVT vecVT = bvNode->getValueType(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000278 DebugLoc dl = bvNode->getDebugLoc();
279
280 // Check to see if this vector can be represented as a CellSPU immediate
281 // constant by invoking all of the instruction selection predicates:
Owen Anderson825b72b2009-08-11 20:47:22 +0000282 if (((vecVT == MVT::v8i16) &&
283 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
284 ((vecVT == MVT::v4i32) &&
285 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
286 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
287 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
Scott Michel7ea02ff2009-03-17 01:15:45 +0000288 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
Owen Anderson825b72b2009-08-11 20:47:22 +0000289 ((vecVT == MVT::v2i64) &&
290 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
291 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
Chris Lattnera8e76142010-02-23 05:30:43 +0000292 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
293 HandleSDNode Dummy(SDValue(bvNode, 0));
294 if (SDNode *N = Select(bvNode))
295 return N;
296 return Dummy.getValue().getNode();
297 }
Scott Michel7ea02ff2009-03-17 01:15:45 +0000298
299 // No, need to emit a constant pool spill:
300 std::vector<Constant*> CV;
301
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000302 for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000303 ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
Chris Lattnera8e76142010-02-23 05:30:43 +0000304 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000305 }
306
Dan Gohman46510a72010-04-15 01:51:59 +0000307 const Constant *CP = ConstantVector::get(CV);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000308 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
309 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
310 SDValue CGPoolOffset =
Dan Gohmand858e902010-04-17 15:26:15 +0000311 SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
Chris Lattnera8e76142010-02-23 05:30:43 +0000312
313 HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
314 CurDAG->getEntryNode(), CGPoolOffset,
315 PseudoSourceValue::getConstantPool(),0,
316 false, false, Alignment));
317 CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
318 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
319 return N;
320 return Dummy.getValue().getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000321 }
Scott Michel02d711b2008-12-30 23:28:25 +0000322
Scott Michel7ea02ff2009-03-17 01:15:45 +0000323 /// Select - Convert the specified operand from a target-independent to a
324 /// target-specific node if it hasn't already been changed.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000325 SDNode *Select(SDNode *N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000326
Scott Michel7ea02ff2009-03-17 01:15:45 +0000327 //! Emit the instruction sequence for i64 shl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000328 SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000329
Scott Michel7ea02ff2009-03-17 01:15:45 +0000330 //! Emit the instruction sequence for i64 srl
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000331 SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000332
Scott Michel7ea02ff2009-03-17 01:15:45 +0000333 //! Emit the instruction sequence for i64 sra
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000334 SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000335
Scott Michel7ea02ff2009-03-17 01:15:45 +0000336 //! Emit the necessary sequence for loading i64 constants:
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000337 SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000338
339 //! Alternate instruction emit sequence for loading i64 constants
Owen Andersone50ed302009-08-10 22:56:29 +0000340 SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000341
342 //! Returns true if the address N is an A-form (local store) address
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000343 bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000344 SDValue &Index);
345
346 //! D-form address predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000347 bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000348 SDValue &Index);
349
350 /// Alternate D-form address using i7 offset predicate
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000351 bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000352 SDValue &Base);
353
354 /// D-form address selection workhorse
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000355 bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000356 SDValue &Base, int minOffset, int maxOffset);
357
358 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000359 bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000360 SDValue &Index);
361
362 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
363 /// inline asm expressions.
364 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
365 char ConstraintCode,
366 std::vector<SDValue> &OutOps) {
367 SDValue Op0, Op1;
368 switch (ConstraintCode) {
369 default: return true;
370 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000371 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
372 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
373 SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000374 break;
375 case 'o': // offsetable
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000376 if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
377 && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
Scott Michel7ea02ff2009-03-17 01:15:45 +0000378 Op0 = Op;
379 Op1 = getSmallIPtrImm(0);
380 }
381 break;
382 case 'v': // not offsetable
383#if 1
Torok Edwinc23197a2009-07-14 16:55:14 +0000384 llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
Scott Michel7ea02ff2009-03-17 01:15:45 +0000385#else
386 SelectAddrIdxOnly(Op, Op, Op0, Op1);
387#endif
388 break;
389 }
390
391 OutOps.push_back(Op0);
392 OutOps.push_back(Op1);
393 return false;
394 }
395
Scott Michel7ea02ff2009-03-17 01:15:45 +0000396 virtual const char *getPassName() const {
397 return "Cell SPU DAG->DAG Pattern Instruction Selection";
398 }
399
400 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
401 /// this target when scheduling the DAG.
402 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
403 const TargetInstrInfo *II = TM.getInstrInfo();
404 assert(II && "No InstrInfo?");
405 return new SPUHazardRecognizer(*II);
406 }
407
408 // Include the pieces autogenerated from the target description.
Scott Michel266bc8f2007-12-04 22:23:35 +0000409#include "SPUGenDAGISel.inc"
Scott Michel7ea02ff2009-03-17 01:15:45 +0000410 };
Dan Gohman844731a2008-05-13 00:00:25 +0000411}
412
Scott Michel266bc8f2007-12-04 22:23:35 +0000413/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000414 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000415 \arg N The address to be tested
416 \arg Base The base address
417 \arg Index The base address index
418 */
419bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000420SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000421 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000422 // These match the addr256k operand type:
Owen Anderson825b72b2009-08-11 20:47:22 +0000423 EVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000424 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000425
426 switch (N.getOpcode()) {
427 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000428 case ISD::ConstantPool:
429 case ISD::GlobalAddress:
Chris Lattner75361b62010-04-07 22:58:41 +0000430 report_fatal_error("SPU SelectAFormAddr: Constant/Pool/Global not lowered.");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000431 /*NOTREACHED*/
432
Scott Michel053c1da2008-01-29 02:16:57 +0000433 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000434 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000435 case ISD::TargetJumpTable:
Chris Lattner75361b62010-04-07 22:58:41 +0000436 report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
Torok Edwindac237e2009-07-08 20:53:28 +0000437 "not wrapped as A-form address.");
Scott Michel053c1da2008-01-29 02:16:57 +0000438 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000439
Scott Michel02d711b2008-12-30 23:28:25 +0000440 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000441 // Just load from memory if there's only a single use of the location,
442 // otherwise, this will get handled below with D-form offset addresses
443 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000444 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000445 switch (Op0.getOpcode()) {
446 case ISD::TargetConstantPool:
447 case ISD::TargetJumpTable:
448 Base = Op0;
449 Index = Zero;
450 return true;
451
452 case ISD::TargetGlobalAddress: {
453 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
Dan Gohman46510a72010-04-15 01:51:59 +0000454 const GlobalValue *GV = GSDN->getGlobal();
Scott Michel053c1da2008-01-29 02:16:57 +0000455 if (GV->getAlignment() == 16) {
456 Base = Op0;
457 Index = Zero;
458 return true;
459 }
460 break;
461 }
462 }
463 }
464 break;
465 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000466 return false;
467}
468
Scott Michel02d711b2008-12-30 23:28:25 +0000469bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000470SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
Dan Gohman475871a2008-07-27 21:46:04 +0000471 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000472 const int minDForm2Offset = -(1 << 7);
473 const int maxDForm2Offset = (1 << 7) - 1;
474 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
475 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000476}
477
Scott Michel266bc8f2007-12-04 22:23:35 +0000478/*!
479 \arg Op The ISD instruction (ignored)
480 \arg N The address to be tested
481 \arg Base Base address register/pointer
482 \arg Index Base address index
483
484 Examine the input address by a base register plus a signed 10-bit
485 displacement, [r+I10] (D-form address).
486
487 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000488 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000489*/
490bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000491SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000492 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000493 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000494 SPUFrameInfo::minFrameOffset(),
495 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000496}
497
498bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000499SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000500 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000501 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000502 unsigned Opc = N.getOpcode();
Owen Andersone50ed302009-08-10 22:56:29 +0000503 EVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000504
Scott Michel053c1da2008-01-29 02:16:57 +0000505 if (Opc == ISD::FrameIndex) {
506 // Stack frame index must be less than 512 (divided by 16):
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000507 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
Scott Michel203b2d62008-04-30 00:30:08 +0000508 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000509 DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000510 << FI << "\n");
511 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000512 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000513 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000514 return true;
515 }
516 } else if (Opc == ISD::ADD) {
517 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000518 const SDValue Op0 = N.getOperand(0);
519 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000520
Scott Michel053c1da2008-01-29 02:16:57 +0000521 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
522 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
523 Base = CurDAG->getTargetConstant(0, PtrTy);
524 Index = N;
525 return true;
526 } else if (Op1.getOpcode() == ISD::Constant
527 || Op1.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000528 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000529 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000530
Scott Michel053c1da2008-01-29 02:16:57 +0000531 if (Op0.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000532 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
Scott Michel203b2d62008-04-30 00:30:08 +0000533 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000534 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000535 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000536
Scott Michel203b2d62008-04-30 00:30:08 +0000537 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000538 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000539 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000540 return true;
541 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000542 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000543 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000544 Index = Op0;
545 return true;
546 }
547 } else if (Op0.getOpcode() == ISD::Constant
548 || Op0.getOpcode() == ISD::TargetConstant) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000549 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000550 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000551
552 if (Op1.getOpcode() == ISD::FrameIndex) {
Dan Gohmanb6f778a2010-04-17 15:31:16 +0000553 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
Scott Michel203b2d62008-04-30 00:30:08 +0000554 int FI = int(FIN->getIndex());
Chris Lattner4437ae22009-08-23 07:05:07 +0000555 DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000556 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000557
Scott Michel203b2d62008-04-30 00:30:08 +0000558 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000559 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000560 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000561 return true;
562 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000563 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000564 Base = CurDAG->getTargetConstant(offset, PtrTy);
565 Index = Op1;
566 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000567 }
Scott Michel053c1da2008-01-29 02:16:57 +0000568 }
569 } else if (Opc == SPUISD::IndirectAddr) {
570 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000571 const SDValue Op0 = N.getOperand(0);
572 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000573
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000574 if (Op0.getOpcode() == SPUISD::Hi
575 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000576 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000577 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000578 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000579 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000580 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
581 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000582 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000583
584 if (isa<ConstantSDNode>(Op1)) {
585 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000586 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000587 idxOp = Op0;
588 } else if (isa<ConstantSDNode>(Op0)) {
589 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000590 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000591 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000592 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000593
594 if (offset >= minOffset && offset <= maxOffset) {
595 Base = CurDAG->getTargetConstant(offset, PtrTy);
596 Index = idxOp;
597 return true;
598 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000599 }
Scott Michel053c1da2008-01-29 02:16:57 +0000600 } else if (Opc == SPUISD::AFormAddr) {
601 Base = CurDAG->getTargetConstant(0, N.getValueType());
602 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000603 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000604 } else if (Opc == SPUISD::LDRESULT) {
605 Base = CurDAG->getTargetConstant(0, N.getValueType());
606 Index = N;
607 return true;
Kalle Raiskilac6166c62010-06-09 08:29:41 +0000608 } else if (Opc == ISD::Register
609 ||Opc == ISD::CopyFromReg
610 ||Opc == ISD::UNDEF) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000611 unsigned OpOpc = Op->getOpcode();
Scott Michel9c0c6b22008-11-21 02:56:16 +0000612
613 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
614 // Direct load/store without getelementptr
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000615 SDValue Offs;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000616
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000617 Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000618
619 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
620 if (Offs.getOpcode() == ISD::UNDEF)
621 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
622
623 Base = Offs;
Kalle Raiskila11fe2462010-06-01 13:34:47 +0000624 Index = N;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000625 return true;
626 }
Scott Michelaedc6372008-12-10 00:15:19 +0000627 } else {
628 /* If otherwise unadorned, default to D-form address with 0 offset: */
629 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000630 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000631 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000632 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000633 }
634
635 Base = CurDAG->getTargetConstant(0, Index.getValueType());
636 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000637 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000638 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000639
Scott Michel266bc8f2007-12-04 22:23:35 +0000640 return false;
641}
642
643/*!
644 \arg Op The ISD instruction operand
645 \arg N The address operand
646 \arg Base The base pointer operand
647 \arg Index The offset/index operand
648
Scott Michel9c0c6b22008-11-21 02:56:16 +0000649 If the address \a N can be expressed as an A-form or D-form address, returns
650 false. Otherwise, creates two operands, Base and Index that will become the
651 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000652*/
653bool
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000654SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +0000655 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000656 if (!SelectAFormAddr(Op, N, Base, Index)
657 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000658 // If the address is neither A-form or D-form, punt and use an X-form
659 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000660 Base = N.getOperand(1);
661 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000662 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000663 }
664
665 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000666}
667
Scott Michel266bc8f2007-12-04 22:23:35 +0000668//! Convert the operand from a target-independent to a target-specific node
669/*!
670 */
671SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000672SPUDAGToDAGISel::Select(SDNode *N) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000673 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000674 int n_ops = -1;
675 unsigned NewOpc;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000676 EVT OpVT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000677 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000678 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000679
Chris Lattnera8e76142010-02-23 05:30:43 +0000680 if (N->isMachineOpcode())
Scott Michel266bc8f2007-12-04 22:23:35 +0000681 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000682
683 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000684 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000685 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
686 SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
Scott Michel266bc8f2007-12-04 22:23:35 +0000687
Scott Michel02d711b2008-12-30 23:28:25 +0000688 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000689 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000690 Ops[0] = TFI;
691 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000692 n_ops = 2;
693 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000694 NewOpc = SPU::Ar32;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000695 Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
Dan Gohman602b0c82009-09-25 18:54:59 +0000696 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000697 N->getValueType(0), TFI, Imm0),
Dan Gohman602b0c82009-09-25 18:54:59 +0000698 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000699 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000700 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000702 // Catch the i64 constants that end up here. Note: The backend doesn't
703 // attempt to legalize the constant (it's useless because DAGCombiner
704 // will insert 64-bit constants and we can't stop it).
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000705 return SelectI64Constant(N, OpVT, N->getDebugLoc());
Scott Michel94bd57e2009-01-15 04:41:47 +0000706 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
Owen Anderson825b72b2009-08-11 20:47:22 +0000707 && OpVT == MVT::i64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000708 SDValue Op0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000709 EVT Op0VT = Op0.getValueType();
Owen Anderson23b9b192009-08-12 00:36:31 +0000710 EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
711 Op0VT, (128 / Op0VT.getSizeInBits()));
712 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
713 OpVT, (128 / OpVT.getSizeInBits()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000714 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000715
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 switch (Op0VT.getSimpleVT().SimpleTy) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000717 default:
Chris Lattner75361b62010-04-07 22:58:41 +0000718 report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
Scott Michel94bd57e2009-01-15 04:41:47 +0000719 /*NOTREACHED*/
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 case MVT::i32:
721 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
722 CurDAG->getConstant(0x80808080, MVT::i32),
723 CurDAG->getConstant(0x00010203, MVT::i32),
724 CurDAG->getConstant(0x80808080, MVT::i32),
725 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000726 break;
727
Owen Anderson825b72b2009-08-11 20:47:22 +0000728 case MVT::i16:
729 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
730 CurDAG->getConstant(0x80808080, MVT::i32),
731 CurDAG->getConstant(0x80800203, MVT::i32),
732 CurDAG->getConstant(0x80808080, MVT::i32),
733 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000734 break;
735
Owen Anderson825b72b2009-08-11 20:47:22 +0000736 case MVT::i8:
737 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
738 CurDAG->getConstant(0x80808080, MVT::i32),
739 CurDAG->getConstant(0x80808003, MVT::i32),
740 CurDAG->getConstant(0x80808080, MVT::i32),
741 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000742 break;
Scott Michel58c58182008-01-17 20:38:41 +0000743 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000744
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000745 SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
Chris Lattnera8e76142010-02-23 05:30:43 +0000746
747 HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
748 Op0VecVT, Op0));
749
750 SDValue PromScalar;
751 if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
752 PromScalar = SDValue(N, 0);
753 else
754 PromScalar = PromoteScalar.getValue();
755
Scott Michel94bd57e2009-01-15 04:41:47 +0000756 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000757 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Chris Lattnera8e76142010-02-23 05:30:43 +0000758 PromScalar, PromScalar,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000759 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000760
Chris Lattnera8e76142010-02-23 05:30:43 +0000761 HandleSDNode Dummy2(zextShuffle);
762 if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
763 zextShuffle = SDValue(N, 0);
764 else
765 zextShuffle = Dummy2.getValue();
766 HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
767 zextShuffle));
768
769 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
770 SelectCode(Dummy.getValue().getNode());
771 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000773 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000774 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000775
Chris Lattnera8e76142010-02-23 05:30:43 +0000776 HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
777 N->getOperand(0), N->getOperand(1),
778 SDValue(CGLoad, 0)));
779
780 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
781 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
782 return N;
783 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000785 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000786 emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000787
Chris Lattnera8e76142010-02-23 05:30:43 +0000788 HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
789 N->getOperand(0), N->getOperand(1),
790 SDValue(CGLoad, 0)));
791
792 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
793 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
794 return N;
795 return Dummy.getValue().getNode();
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
Scott Michel94bd57e2009-01-15 04:41:47 +0000797 SDNode *CGLoad =
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000798 emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
Scott Michel94bd57e2009-01-15 04:41:47 +0000799
Chris Lattnera8e76142010-02-23 05:30:43 +0000800 HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
801 N->getOperand(0), N->getOperand(1),
802 SDValue(CGLoad, 0)));
803 CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
804 if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
805 return N;
806 return Dummy.getValue().getNode();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000807 } else if (Opc == ISD::TRUNCATE) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000808 SDValue Op0 = N->getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000809 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 && OpVT == MVT::i32
811 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000812 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
813 //
814 // Take advantage of the fact that the upper 32 bits are in the
815 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000816 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
817 if (CN != 0) {
818 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000819
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000820 if (shift_amt >= 32) {
821 SDNode *hi32 =
Dan Gohman602b0c82009-09-25 18:54:59 +0000822 CurDAG->getMachineNode(SPU::ORr32_r64, dl, OpVT,
823 Op0.getOperand(0));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000824
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000825 shift_amt -= 32;
826 if (shift_amt > 0) {
827 // Take care of the additional shift, if present:
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000829 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000830
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000831 if (Op0.getOpcode() == ISD::SRL)
832 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000833
Dan Gohman602b0c82009-09-25 18:54:59 +0000834 hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
835 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000836 }
837
838 return hi32;
839 }
840 }
841 }
Scott Michel02d711b2008-12-30 23:28:25 +0000842 } else if (Opc == ISD::SHL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000843 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000844 return SelectSHLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000845 } else if (Opc == ISD::SRL) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000846 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000847 return SelectSRLi64(N, OpVT);
Scott Michel02d711b2008-12-30 23:28:25 +0000848 } else if (Opc == ISD::SRA) {
Chris Lattnera8e76142010-02-23 05:30:43 +0000849 if (OpVT == MVT::i64)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000850 return SelectSRAi64(N, OpVT);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000851 } else if (Opc == ISD::FNEG
Owen Anderson825b72b2009-08-11 20:47:22 +0000852 && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000853 DebugLoc dl = N->getDebugLoc();
Scott Michel7ea02ff2009-03-17 01:15:45 +0000854 // Check if the pattern is a special form of DFNMS:
855 // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000856 SDValue Op0 = N->getOperand(0);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000857 if (Op0.getOpcode() == ISD::FSUB) {
858 SDValue Op00 = Op0.getOperand(0);
859 if (Op00.getOpcode() == ISD::FMUL) {
860 unsigned Opc = SPU::DFNMSf64;
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 if (OpVT == MVT::v2f64)
Scott Michel7ea02ff2009-03-17 01:15:45 +0000862 Opc = SPU::DFNMSv2f64;
863
Dan Gohman602b0c82009-09-25 18:54:59 +0000864 return CurDAG->getMachineNode(Opc, dl, OpVT,
865 Op00.getOperand(0),
866 Op00.getOperand(1),
867 Op0.getOperand(1));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000868 }
869 }
870
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
Scott Michel7ea02ff2009-03-17 01:15:45 +0000872 SDNode *signMask = 0;
Scott Michela82d3f72009-03-17 16:45:16 +0000873 unsigned Opc = SPU::XORfneg64;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000874
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 if (OpVT == MVT::f64) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000876 signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +0000877 } else if (OpVT == MVT::v2f64) {
Scott Michela82d3f72009-03-17 16:45:16 +0000878 Opc = SPU::XORfnegvec;
Scott Michel7ea02ff2009-03-17 01:15:45 +0000879 signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +0000880 MVT::v2i64,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000881 negConst, negConst).getNode());
Scott Michel7ea02ff2009-03-17 01:15:45 +0000882 }
883
Dan Gohman602b0c82009-09-25 18:54:59 +0000884 return CurDAG->getMachineNode(Opc, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000885 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000886 } else if (Opc == ISD::FABS) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 if (OpVT == MVT::f64) {
888 SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
Dan Gohman602b0c82009-09-25 18:54:59 +0000889 return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000890 N->getOperand(0), SDValue(signMask, 0));
Owen Anderson825b72b2009-08-11 20:47:22 +0000891 } else if (OpVT == MVT::v2f64) {
892 SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
893 SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
Scott Michel7ea02ff2009-03-17 01:15:45 +0000894 absConst, absConst);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000895 SDNode *signMask = emitBuildVector(absVec.getNode());
Dan Gohman602b0c82009-09-25 18:54:59 +0000896 return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000897 N->getOperand(0), SDValue(signMask, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +0000898 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000899 } else if (Opc == SPUISD::LDRESULT) {
900 // Custom select instructions for LDRESULT
Owen Andersone50ed302009-08-10 22:56:29 +0000901 EVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000902 SDValue Arg = N->getOperand(0);
903 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000904 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000905 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
906
907 if (vtm->ldresult_ins == 0) {
Benjamin Kramer1bd73352010-04-08 10:44:28 +0000908 report_fatal_error("LDRESULT for unsupported type: " +
909 Twine(VT.getEVTString()));
Scott Michela59d4692008-02-23 18:41:37 +0000910 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000911
Scott Michela59d4692008-02-23 18:41:37 +0000912 Opc = vtm->ldresult_ins;
913 if (vtm->ldresult_imm) {
Dan Gohman475871a2008-07-27 21:46:04 +0000914 SDValue Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000915
Dan Gohman602b0c82009-09-25 18:54:59 +0000916 Result = CurDAG->getMachineNode(Opc, dl, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000917 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +0000918 Result = CurDAG->getMachineNode(Opc, dl, VT, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000919 }
920
Scott Michel266bc8f2007-12-04 22:23:35 +0000921 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000922 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000923 // Look at the operands: SelectCode() will catch the cases that aren't
924 // specifically handled here.
925 //
926 // SPUInstrInfo catches the following patterns:
927 // (SPUindirect (SPUhi ...), (SPUlo ...))
928 // (SPUindirect $sp, imm)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000929 EVT VT = N->getValueType(0);
Scott Michelf0569be2008-12-27 04:51:36 +0000930 SDValue Op0 = N->getOperand(0);
931 SDValue Op1 = N->getOperand(1);
932 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000933
Scott Michelf0569be2008-12-27 04:51:36 +0000934 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
935 || (Op0.getOpcode() == ISD::Register
936 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
937 && RN->getReg() != SPU::R1))) {
938 NewOpc = SPU::Ar32;
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000939 Ops[1] = Op1;
Scott Michel58c58182008-01-17 20:38:41 +0000940 if (Op1.getOpcode() == ISD::Constant) {
941 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000942 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Chris Lattnerd4ac35b2010-05-04 17:58:46 +0000943 if (isInt<10>(CN->getSExtValue())) {
944 NewOpc = SPU::AIr32;
945 Ops[1] = Op1;
946 } else {
947 Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
948 N->getValueType(0),
949 Op1),
950 0);
951 }
Scott Michel58c58182008-01-17 20:38:41 +0000952 }
Scott Michelf0569be2008-12-27 04:51:36 +0000953 Ops[0] = Op0;
Scott Michelf0569be2008-12-27 04:51:36 +0000954 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000955 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000956 }
Scott Michel02d711b2008-12-30 23:28:25 +0000957
Scott Michel58c58182008-01-17 20:38:41 +0000958 if (n_ops > 0) {
959 if (N->hasOneUse())
960 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
961 else
Dan Gohman602b0c82009-09-25 18:54:59 +0000962 return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000963 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000964 return SelectCode(N);
Scott Michel266bc8f2007-12-04 22:23:35 +0000965}
966
Scott Michel02d711b2008-12-30 23:28:25 +0000967/*!
968 * Emit the instruction sequence for i64 left shifts. The basic algorithm
969 * is to fill the bottom two word slots with zeros so that zeros are shifted
970 * in as the entire quadword is shifted left.
971 *
972 * \note This code could also be used to implement v2i64 shl.
973 *
974 * @param Op The shl operand
975 * @param OpVT Op's machine value value type (doesn't need to be passed, but
976 * makes life easier.)
977 * @return The SDNode with the entire instruction sequence
978 */
979SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000980SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
981 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +0000982 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
983 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000984 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +0000985 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +0000986 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
987 SDValue SelMaskVal;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000988 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000989
Dan Gohman602b0c82009-09-25 18:54:59 +0000990 VecOp0 = CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
Owen Anderson825b72b2009-08-11 20:47:22 +0000991 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dan Gohman602b0c82009-09-25 18:54:59 +0000992 SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
993 ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
994 CurDAG->getTargetConstant(0, OpVT));
995 VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
996 SDValue(ZeroFill, 0),
997 SDValue(VecOp0, 0),
998 SDValue(SelMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000999
1000 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1001 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1002 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1003
1004 if (bytes > 0) {
1005 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001006 CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
1007 SDValue(VecOp0, 0),
1008 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001009 }
1010
1011 if (bits > 0) {
1012 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001013 CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
1014 SDValue((Shift != 0 ? Shift : VecOp0), 0),
1015 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001016 }
1017 } else {
1018 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001019 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1020 ShiftAmt,
1021 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001022 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001023 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1024 ShiftAmt,
1025 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001026 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001027 CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
1028 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001029 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001030 CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
1031 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001032 }
1033
Dan Gohman602b0c82009-09-25 18:54:59 +00001034 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001035}
1036
1037/*!
1038 * Emit the instruction sequence for i64 logical right shifts.
1039 *
1040 * @param Op The shl operand
1041 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1042 * makes life easier.)
1043 * @return The SDNode with the entire instruction sequence
1044 */
1045SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001046SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
1047 SDValue Op0 = N->getOperand(0);
Owen Anderson23b9b192009-08-12 00:36:31 +00001048 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1049 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001050 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001051 EVT ShiftAmtVT = ShiftAmt.getValueType();
Scott Michel02d711b2008-12-30 23:28:25 +00001052 SDNode *VecOp0, *Shift = 0;
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001053 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001054
Dan Gohman602b0c82009-09-25 18:54:59 +00001055 VecOp0 = CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
Scott Michel02d711b2008-12-30 23:28:25 +00001056
1057 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1058 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1059 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1060
1061 if (bytes > 0) {
1062 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001063 CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
1064 SDValue(VecOp0, 0),
1065 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001066 }
1067
1068 if (bits > 0) {
1069 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001070 CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
1071 SDValue((Shift != 0 ? Shift : VecOp0), 0),
1072 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001073 }
1074 } else {
1075 SDNode *Bytes =
Dan Gohman602b0c82009-09-25 18:54:59 +00001076 CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1077 ShiftAmt,
1078 CurDAG->getTargetConstant(3, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001079 SDNode *Bits =
Dan Gohman602b0c82009-09-25 18:54:59 +00001080 CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1081 ShiftAmt,
1082 CurDAG->getTargetConstant(7, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001083
1084 // Ensure that the shift amounts are negated!
Dan Gohman602b0c82009-09-25 18:54:59 +00001085 Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1086 SDValue(Bytes, 0),
1087 CurDAG->getTargetConstant(0, ShiftAmtVT));
1088
1089 Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1090 SDValue(Bits, 0),
Scott Michel02d711b2008-12-30 23:28:25 +00001091 CurDAG->getTargetConstant(0, ShiftAmtVT));
1092
Scott Michel02d711b2008-12-30 23:28:25 +00001093 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001094 CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1095 SDValue(VecOp0, 0), SDValue(Bytes, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001096 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001097 CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1098 SDValue(Shift, 0), SDValue(Bits, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001099 }
1100
Dan Gohman602b0c82009-09-25 18:54:59 +00001101 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001102}
1103
1104/*!
1105 * Emit the instruction sequence for i64 arithmetic right shifts.
1106 *
1107 * @param Op The shl operand
1108 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1109 * makes life easier.)
1110 * @return The SDNode with the entire instruction sequence
1111 */
1112SDNode *
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001113SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
Scott Michel02d711b2008-12-30 23:28:25 +00001114 // Promote Op0 to vector
Owen Anderson23b9b192009-08-12 00:36:31 +00001115 EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1116 OpVT, (128 / OpVT.getSizeInBits()));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001117 SDValue ShiftAmt = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001118 EVT ShiftAmtVT = ShiftAmt.getValueType();
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001119 DebugLoc dl = N->getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001120
1121 SDNode *VecOp0 =
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001122 CurDAG->getMachineNode(SPU::ORv2i64_i64, dl, VecVT, N->getOperand(0));
Scott Michel02d711b2008-12-30 23:28:25 +00001123
1124 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1125 SDNode *SignRot =
Dan Gohman602b0c82009-09-25 18:54:59 +00001126 CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1127 SDValue(VecOp0, 0), SignRotAmt);
Scott Michel02d711b2008-12-30 23:28:25 +00001128 SDNode *UpperHalfSign =
Dan Gohman602b0c82009-09-25 18:54:59 +00001129 CurDAG->getMachineNode(SPU::ORi32_v4i32, dl, MVT::i32, SDValue(SignRot, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001130
1131 SDNode *UpperHalfSignMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001132 CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001133 SDNode *UpperLowerMask =
Dan Gohman602b0c82009-09-25 18:54:59 +00001134 CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1135 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
Scott Michel02d711b2008-12-30 23:28:25 +00001136 SDNode *UpperLowerSelect =
Dan Gohman602b0c82009-09-25 18:54:59 +00001137 CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1138 SDValue(UpperHalfSignMask, 0),
1139 SDValue(VecOp0, 0),
1140 SDValue(UpperLowerMask, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001141
1142 SDNode *Shift = 0;
1143
1144 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1145 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1146 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1147
1148 if (bytes > 0) {
1149 bytes = 31 - bytes;
1150 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001151 CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1152 SDValue(UpperLowerSelect, 0),
1153 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001154 }
1155
1156 if (bits > 0) {
1157 bits = 8 - bits;
1158 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001159 CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1160 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1161 CurDAG->getTargetConstant(bits, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001162 }
1163 } else {
1164 SDNode *NegShift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001165 CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1166 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
Scott Michel02d711b2008-12-30 23:28:25 +00001167
1168 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001169 CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1170 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001171 Shift =
Dan Gohman602b0c82009-09-25 18:54:59 +00001172 CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1173 SDValue(Shift, 0), SDValue(NegShift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001174 }
1175
Dan Gohman602b0c82009-09-25 18:54:59 +00001176 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001177}
1178
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001179/*!
1180 Do the necessary magic necessary to load a i64 constant
1181 */
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001182SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001183 DebugLoc dl) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001184 ConstantSDNode *CN = cast<ConstantSDNode>(N);
Scott Michel7ea02ff2009-03-17 01:15:45 +00001185 return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1186}
1187
Owen Andersone50ed302009-08-10 22:56:29 +00001188SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
Scott Michel7ea02ff2009-03-17 01:15:45 +00001189 DebugLoc dl) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001190 EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001191 SDValue i64vec =
Scott Michel7ea02ff2009-03-17 01:15:45 +00001192 SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001193
1194 // Here's where it gets interesting, because we have to parse out the
1195 // subtree handed back in i64vec:
1196
1197 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1198 // The degenerate case where the upper and lower bits in the splat are
1199 // identical:
1200 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001201
Scott Michel9de57a92009-01-26 22:33:37 +00001202 ReplaceUses(i64vec, Op0);
Dan Gohman602b0c82009-09-25 18:54:59 +00001203 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001204 SDValue(emitBuildVector(Op0.getNode()), 0));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001205 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1206 SDValue lhs = i64vec.getOperand(0);
1207 SDValue rhs = i64vec.getOperand(1);
1208 SDValue shufmask = i64vec.getOperand(2);
1209
1210 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1211 ReplaceUses(lhs, lhs.getOperand(0));
1212 lhs = lhs.getOperand(0);
1213 }
1214
1215 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1216 ? lhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001217 : emitBuildVector(lhs.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001218
1219 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1220 ReplaceUses(rhs, rhs.getOperand(0));
1221 rhs = rhs.getOperand(0);
1222 }
1223
1224 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1225 ? rhs.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001226 : emitBuildVector(rhs.getNode()));
Scott Michel9de57a92009-01-26 22:33:37 +00001227
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001228 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1229 ReplaceUses(shufmask, shufmask.getOperand(0));
1230 shufmask = shufmask.getOperand(0);
1231 }
1232
1233 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1234 ? shufmask.getNode()
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001235 : emitBuildVector(shufmask.getNode()));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001236
Chris Lattnera8e76142010-02-23 05:30:43 +00001237 SDValue shufNode =
1238 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001239 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
Chris Lattnera8e76142010-02-23 05:30:43 +00001240 SDValue(shufMaskNode, 0));
1241 HandleSDNode Dummy(shufNode);
1242 SDNode *SN = SelectCode(Dummy.getValue().getNode());
1243 if (SN == 0) SN = Dummy.getValue().getNode();
1244
1245 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(SN, 0));
Scott Michel7ea02ff2009-03-17 01:15:45 +00001246 } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001247 return CurDAG->getMachineNode(SPU::ORi64_v2i64, dl, OpVT,
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001248 SDValue(emitBuildVector(i64vec.getNode()), 0));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001249 } else {
Chris Lattner75361b62010-04-07 22:58:41 +00001250 report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
Torok Edwindac237e2009-07-08 20:53:28 +00001251 "condition");
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001252 }
1253}
1254
Scott Michel02d711b2008-12-30 23:28:25 +00001255/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001256/// SPU-specific DAG, ready for instruction scheduling.
1257///
1258FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1259 return new SPUDAGToDAGISel(TM);
1260}