blob: 23bb08c0c07c0ee8774c31094876811e4cf7be27 [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"
17#include "SPUISelLowering.h"
18#include "SPUHazardRecognizers.h"
19#include "SPUFrameInfo.h"
Scott Michel203b2d62008-04-30 00:30:08 +000020#include "SPURegisterNames.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000021#include "SPUTargetMachine.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000022#include "llvm/CodeGen/MachineConstantPool.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineFunction.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
Scott Michel94bd57e2009-01-15 04:41:47 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000028#include "llvm/Target/TargetOptions.h"
29#include "llvm/ADT/Statistic.h"
30#include "llvm/Constants.h"
31#include "llvm/GlobalValue.h"
32#include "llvm/Intrinsics.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/MathExtras.h"
35#include "llvm/Support/Compiler.h"
Scott Michel266bc8f2007-12-04 22:23:35 +000036
37using namespace llvm;
38
39namespace {
40 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
41 bool
42 isI64IntS10Immediate(ConstantSDNode *CN)
43 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000044 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000045 }
46
47 //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
48 bool
49 isI32IntS10Immediate(ConstantSDNode *CN)
50 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000051 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000052 }
53
Scott Michel504c3692007-12-17 22:32:34 +000054 //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
55 bool
56 isI32IntU10Immediate(ConstantSDNode *CN)
57 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000058 return isU10Constant(CN->getSExtValue());
Scott Michel504c3692007-12-17 22:32:34 +000059 }
60
Scott Michel266bc8f2007-12-04 22:23:35 +000061 //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
62 bool
63 isI16IntS10Immediate(ConstantSDNode *CN)
64 {
Dan Gohman7810bfe2008-09-26 21:54:37 +000065 return isS10Constant(CN->getSExtValue());
Scott Michel266bc8f2007-12-04 22:23:35 +000066 }
67
68 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
69 bool
70 isI16IntS10Immediate(SDNode *N)
71 {
Scott Michel9de57a92009-01-26 22:33:37 +000072 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
73 return (CN != 0 && isI16IntS10Immediate(CN));
Scott Michel266bc8f2007-12-04 22:23:35 +000074 }
75
Scott Michelec2a08f2007-12-15 00:38:50 +000076 //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
77 bool
78 isI16IntU10Immediate(ConstantSDNode *CN)
79 {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000080 return isU10Constant((short) CN->getZExtValue());
Scott Michelec2a08f2007-12-15 00:38:50 +000081 }
82
83 //! SDNode predicate for i16 sign-extended, 10-bit immediate values
84 bool
85 isI16IntU10Immediate(SDNode *N)
86 {
87 return (N->getOpcode() == ISD::Constant
88 && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
89 }
90
Scott Michel266bc8f2007-12-04 22:23:35 +000091 //! ConstantSDNode predicate for signed 16-bit values
92 /*!
93 \arg CN The constant SelectionDAG node holding the value
94 \arg Imm The returned 16-bit value, if returning true
95
96 This predicate tests the value in \a CN to see whether it can be
97 represented as a 16-bit, sign-extended quantity. Returns true if
98 this is the case.
99 */
100 bool
101 isIntS16Immediate(ConstantSDNode *CN, short &Imm)
102 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000103 MVT vt = CN->getValueType(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000104 Imm = (short) CN->getZExtValue();
Duncan Sands8e4eb092008-06-08 20:54:56 +0000105 if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000106 return true;
107 } else if (vt == MVT::i32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000108 int32_t i_val = (int32_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000109 short s_val = (short) i_val;
110 return i_val == s_val;
111 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000112 int64_t i_val = (int64_t) CN->getZExtValue();
Scott Michel266bc8f2007-12-04 22:23:35 +0000113 short s_val = (short) i_val;
114 return i_val == s_val;
115 }
116
117 return false;
118 }
119
120 //! SDNode predicate for signed 16-bit values.
121 bool
122 isIntS16Immediate(SDNode *N, short &Imm)
123 {
124 return (N->getOpcode() == ISD::Constant
125 && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
126 }
127
128 //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
129 static bool
130 isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
131 {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000132 MVT vt = FPN->getValueType(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000133 if (vt == MVT::f32) {
Chris Lattnerd3ada752007-12-22 22:45:38 +0000134 int val = FloatToBits(FPN->getValueAPF().convertToFloat());
Scott Michel266bc8f2007-12-04 22:23:35 +0000135 int sval = (int) ((val << 16) >> 16);
136 Imm = (short) val;
137 return val == sval;
138 }
139
140 return false;
141 }
142
Scott Michel053c1da2008-01-29 02:16:57 +0000143 bool
Scott Michel02d711b2008-12-30 23:28:25 +0000144 isHighLow(const SDValue &Op)
Scott Michel053c1da2008-01-29 02:16:57 +0000145 {
146 return (Op.getOpcode() == SPUISD::IndirectAddr
147 && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
148 && Op.getOperand(1).getOpcode() == SPUISD::Lo)
149 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
150 && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
151 }
152
Scott Michel266bc8f2007-12-04 22:23:35 +0000153 //===------------------------------------------------------------------===//
Duncan Sands83ec4b62008-06-06 12:08:01 +0000154 //! MVT to "useful stuff" mapping structure:
Scott Michel266bc8f2007-12-04 22:23:35 +0000155
156 struct valtype_map_s {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000157 MVT VT;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000158 unsigned ldresult_ins; /// LDRESULT instruction (0 = undefined)
Scott Michela59d4692008-02-23 18:41:37 +0000159 bool ldresult_imm; /// LDRESULT instruction requires immediate?
Scott Michelf0569be2008-12-27 04:51:36 +0000160 unsigned lrinst; /// LR instruction
Scott Michel266bc8f2007-12-04 22:23:35 +0000161 };
162
163 const valtype_map_s valtype_map[] = {
Scott Michelf0569be2008-12-27 04:51:36 +0000164 { MVT::i8, SPU::ORBIr8, true, SPU::LRr8 },
165 { MVT::i16, SPU::ORHIr16, true, SPU::LRr16 },
166 { MVT::i32, SPU::ORIr32, true, SPU::LRr32 },
167 { MVT::i64, SPU::ORr64, false, SPU::LRr64 },
168 { MVT::f32, SPU::ORf32, false, SPU::LRf32 },
169 { MVT::f64, SPU::ORf64, false, SPU::LRf64 },
Scott Michel58c58182008-01-17 20:38:41 +0000170 // vector types... (sigh!)
Scott Michelf0569be2008-12-27 04:51:36 +0000171 { MVT::v16i8, 0, false, SPU::LRv16i8 },
172 { MVT::v8i16, 0, false, SPU::LRv8i16 },
173 { MVT::v4i32, 0, false, SPU::LRv4i32 },
174 { MVT::v2i64, 0, false, SPU::LRv2i64 },
175 { MVT::v4f32, 0, false, SPU::LRv4f32 },
176 { MVT::v2f64, 0, false, SPU::LRv2f64 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000177 };
178
179 const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
180
Duncan Sands83ec4b62008-06-06 12:08:01 +0000181 const valtype_map_s *getValueTypeMapEntry(MVT VT)
Scott Michel266bc8f2007-12-04 22:23:35 +0000182 {
183 const valtype_map_s *retval = 0;
184 for (size_t i = 0; i < n_valtype_map; ++i) {
185 if (valtype_map[i].VT == VT) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000186 retval = valtype_map + i;
187 break;
Scott Michel266bc8f2007-12-04 22:23:35 +0000188 }
189 }
190
191
192#ifndef NDEBUG
193 if (retval == 0) {
194 cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000195 << VT.getMVTString()
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000196 << "\n";
Scott Michel266bc8f2007-12-04 22:23:35 +0000197 abort();
198 }
199#endif
200
201 return retval;
202 }
203}
204
Dan Gohman844731a2008-05-13 00:00:25 +0000205namespace {
206
Scott Michel266bc8f2007-12-04 22:23:35 +0000207//===--------------------------------------------------------------------===//
208/// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
209/// instructions for SelectionDAG operations.
210///
211class SPUDAGToDAGISel :
212 public SelectionDAGISel
213{
214 SPUTargetMachine &TM;
215 SPUTargetLowering &SPUtli;
216 unsigned GlobalBaseReg;
217
218public:
Dan Gohman1002c022008-07-07 18:00:37 +0000219 explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
Dan Gohman79ce2762009-01-15 19:20:50 +0000220 SelectionDAGISel(tm),
Scott Michel266bc8f2007-12-04 22:23:35 +0000221 TM(tm),
222 SPUtli(*tm.getTargetLowering())
Scott Michel9de57a92009-01-26 22:33:37 +0000223 { }
Scott Michel02d711b2008-12-30 23:28:25 +0000224
Scott Michel266bc8f2007-12-04 22:23:35 +0000225 virtual bool runOnFunction(Function &Fn) {
226 // Make sure we re-emit a set of the global base reg if necessary
227 GlobalBaseReg = 0;
228 SelectionDAGISel::runOnFunction(Fn);
229 return true;
230 }
Scott Michel02d711b2008-12-30 23:28:25 +0000231
Scott Michel266bc8f2007-12-04 22:23:35 +0000232 /// getI32Imm - Return a target constant with the specified value, of type
233 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000234 inline SDValue getI32Imm(uint32_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000235 return CurDAG->getTargetConstant(Imm, MVT::i32);
236 }
237
238 /// getI64Imm - Return a target constant with the specified value, of type
239 /// i64.
Dan Gohman475871a2008-07-27 21:46:04 +0000240 inline SDValue getI64Imm(uint64_t Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000241 return CurDAG->getTargetConstant(Imm, MVT::i64);
242 }
Scott Michel02d711b2008-12-30 23:28:25 +0000243
Scott Michel266bc8f2007-12-04 22:23:35 +0000244 /// getSmallIPtrImm - Return a target constant of pointer type.
Dan Gohman475871a2008-07-27 21:46:04 +0000245 inline SDValue getSmallIPtrImm(unsigned Imm) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000246 return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000247 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000248
Scott Michel94bd57e2009-01-15 04:41:47 +0000249 SDNode *emitBuildVector(SDValue build_vec) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000250 MVT vecVT = build_vec.getValueType();
251 SDNode *bvNode = build_vec.getNode();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000252 DebugLoc dl = bvNode->getDebugLoc();
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000253
254 // Check to see if this vector can be represented as a CellSPU immediate
Scott Michel9de57a92009-01-26 22:33:37 +0000255 // constant by invoking all of the instruction selection predicates:
256 if (((vecVT == MVT::v8i16) &&
257 (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
258 ((vecVT == MVT::v4i32) &&
259 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
260 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
261 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
262 (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
263 ((vecVT == MVT::v2i64) &&
264 ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
265 (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
266 (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0))))
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000267 return Select(build_vec);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000268
269 // No, need to emit a constant pool spill:
Scott Michel94bd57e2009-01-15 04:41:47 +0000270 std::vector<Constant*> CV;
271
272 for (size_t i = 0; i < build_vec.getNumOperands(); ++i) {
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000273 ConstantSDNode *V = dyn_cast<ConstantSDNode > (build_vec.getOperand(i));
274 CV.push_back(const_cast<ConstantInt *> (V->getConstantIntValue()));
Scott Michel94bd57e2009-01-15 04:41:47 +0000275 }
276
277 Constant *CP = ConstantVector::get(CV);
278 SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000279 unsigned Alignment = 1 << cast<ConstantPoolSDNode > (CPIdx)->getAlignment();
Scott Michel94bd57e2009-01-15 04:41:47 +0000280 SDValue CGPoolOffset =
281 SPU::LowerConstantPool(CPIdx, *CurDAG,
282 SPUtli.getSPUTargetMachine());
Dale Johannesen33c960f2009-02-04 20:06:27 +0000283 return SelectCode(CurDAG->getLoad(build_vec.getValueType(), dl,
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000284 CurDAG->getEntryNode(), CGPoolOffset,
285 PseudoSourceValue::getConstantPool(), 0,
286 false, Alignment));
Scott Michel94bd57e2009-01-15 04:41:47 +0000287 }
288
Scott Michel266bc8f2007-12-04 22:23:35 +0000289 /// Select - Convert the specified operand from a target-independent to a
290 /// target-specific node if it hasn't already been changed.
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDNode *Select(SDValue Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000292
Scott Michel02d711b2008-12-30 23:28:25 +0000293 //! Emit the instruction sequence for i64 shl
294 SDNode *SelectSHLi64(SDValue &Op, MVT OpVT);
295
296 //! Emit the instruction sequence for i64 srl
297 SDNode *SelectSRLi64(SDValue &Op, MVT OpVT);
298
299 //! Emit the instruction sequence for i64 sra
300 SDNode *SelectSRAi64(SDValue &Op, MVT OpVT);
301
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000302 //! Emit the necessary sequence for loading i64 constants:
303 SDNode *SelectI64Constant(SDValue &Op, MVT OpVT);
304
Scott Michel266bc8f2007-12-04 22:23:35 +0000305 //! Returns true if the address N is an A-form (local store) address
Dan Gohman475871a2008-07-27 21:46:04 +0000306 bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
307 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000308
309 //! D-form address predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000310 bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
311 SDValue &Index);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000312
313 /// Alternate D-form address using i7 offset predicate
Dan Gohman475871a2008-07-27 21:46:04 +0000314 bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
315 SDValue &Base);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000316
317 /// D-form address selection workhorse
Dan Gohman475871a2008-07-27 21:46:04 +0000318 bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp,
319 SDValue &Base, int minOffset, int maxOffset);
Scott Michel266bc8f2007-12-04 22:23:35 +0000320
321 //! Address predicate if N can be expressed as an indexed [r+r] operation.
Dan Gohman475871a2008-07-27 21:46:04 +0000322 bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
323 SDValue &Index);
Scott Michel266bc8f2007-12-04 22:23:35 +0000324
325 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
326 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000327 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000328 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000329 std::vector<SDValue> &OutOps) {
Dan Gohman475871a2008-07-27 21:46:04 +0000330 SDValue Op0, Op1;
Scott Michel266bc8f2007-12-04 22:23:35 +0000331 switch (ConstraintCode) {
332 default: return true;
333 case 'm': // memory
Scott Michel02d711b2008-12-30 23:28:25 +0000334 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000335 && !SelectAFormAddr(Op, Op, Op0, Op1))
336 SelectXFormAddr(Op, Op, Op0, Op1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000337 break;
338 case 'o': // offsetable
339 if (!SelectDFormAddr(Op, Op, Op0, Op1)
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000340 && !SelectAFormAddr(Op, Op, Op0, Op1)) {
341 Op0 = Op;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000342 Op1 = getSmallIPtrImm(0);
Scott Michel266bc8f2007-12-04 22:23:35 +0000343 }
344 break;
345 case 'v': // not offsetable
346#if 1
347 assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
348#else
349 SelectAddrIdxOnly(Op, Op, Op0, Op1);
350#endif
351 break;
352 }
Scott Michel02d711b2008-12-30 23:28:25 +0000353
Scott Michel266bc8f2007-12-04 22:23:35 +0000354 OutOps.push_back(Op0);
355 OutOps.push_back(Op1);
356 return false;
357 }
358
Evan Chengdb8d56b2008-06-30 20:45:06 +0000359 /// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000360 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000361 virtual void InstructionSelect();
Scott Michel266bc8f2007-12-04 22:23:35 +0000362
363 virtual const char *getPassName() const {
364 return "Cell SPU DAG->DAG Pattern Instruction Selection";
Scott Michel02d711b2008-12-30 23:28:25 +0000365 }
366
Scott Michel266bc8f2007-12-04 22:23:35 +0000367 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
368 /// this target when scheduling the DAG.
Dan Gohmanfc54c552009-01-15 22:18:12 +0000369 virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
Dan Gohman6448d912008-09-04 15:39:15 +0000370 const TargetInstrInfo *II = TM.getInstrInfo();
Scott Michel266bc8f2007-12-04 22:23:35 +0000371 assert(II && "No InstrInfo?");
Scott Michel02d711b2008-12-30 23:28:25 +0000372 return new SPUHazardRecognizer(*II);
Scott Michel266bc8f2007-12-04 22:23:35 +0000373 }
374
375 // Include the pieces autogenerated from the target description.
376#include "SPUGenDAGISel.inc"
377};
378
Dan Gohman844731a2008-05-13 00:00:25 +0000379}
380
Evan Chengdb8d56b2008-06-30 20:45:06 +0000381/// InstructionSelect - This callback is invoked by
Scott Michel266bc8f2007-12-04 22:23:35 +0000382/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
383void
Dan Gohmanf350b272008-08-23 02:25:05 +0000384SPUDAGToDAGISel::InstructionSelect()
Scott Michel266bc8f2007-12-04 22:23:35 +0000385{
386 DEBUG(BB->dump());
387
388 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +0000389 SelectRoot(*CurDAG);
Dan Gohmanf350b272008-08-23 02:25:05 +0000390 CurDAG->RemoveDeadNodes();
Scott Michel266bc8f2007-12-04 22:23:35 +0000391}
392
Scott Michel266bc8f2007-12-04 22:23:35 +0000393/*!
Scott Michel9de57a92009-01-26 22:33:37 +0000394 \arg Op The ISD instruction operand
Scott Michel266bc8f2007-12-04 22:23:35 +0000395 \arg N The address to be tested
396 \arg Base The base address
397 \arg Index The base address index
398 */
399bool
Dan Gohman475871a2008-07-27 21:46:04 +0000400SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
401 SDValue &Index) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000402 // These match the addr256k operand type:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000403 MVT OffsVT = MVT::i16;
Dan Gohman475871a2008-07-27 21:46:04 +0000404 SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
Scott Michel266bc8f2007-12-04 22:23:35 +0000405
406 switch (N.getOpcode()) {
407 case ISD::Constant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000408 case ISD::ConstantPool:
409 case ISD::GlobalAddress:
410 cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
411 abort();
412 /*NOTREACHED*/
413
Scott Michel053c1da2008-01-29 02:16:57 +0000414 case ISD::TargetConstant:
Scott Michel9de5d0d2008-01-11 02:53:15 +0000415 case ISD::TargetGlobalAddress:
Scott Michel053c1da2008-01-29 02:16:57 +0000416 case ISD::TargetJumpTable:
417 cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
418 << "A-form address.\n";
419 abort();
420 /*NOTREACHED*/
Scott Michel266bc8f2007-12-04 22:23:35 +0000421
Scott Michel02d711b2008-12-30 23:28:25 +0000422 case SPUISD::AFormAddr:
Scott Michel053c1da2008-01-29 02:16:57 +0000423 // Just load from memory if there's only a single use of the location,
424 // otherwise, this will get handled below with D-form offset addresses
425 if (N.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000426 SDValue Op0 = N.getOperand(0);
Scott Michel053c1da2008-01-29 02:16:57 +0000427 switch (Op0.getOpcode()) {
428 case ISD::TargetConstantPool:
429 case ISD::TargetJumpTable:
430 Base = Op0;
431 Index = Zero;
432 return true;
433
434 case ISD::TargetGlobalAddress: {
435 GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
436 GlobalValue *GV = GSDN->getGlobal();
437 if (GV->getAlignment() == 16) {
438 Base = Op0;
439 Index = Zero;
440 return true;
441 }
442 break;
443 }
444 }
445 }
446 break;
447 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000448 return false;
449}
450
Scott Michel02d711b2008-12-30 23:28:25 +0000451bool
Dan Gohman475871a2008-07-27 21:46:04 +0000452SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
453 SDValue &Base) {
Scott Michel203b2d62008-04-30 00:30:08 +0000454 const int minDForm2Offset = -(1 << 7);
455 const int maxDForm2Offset = (1 << 7) - 1;
456 return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
457 maxDForm2Offset);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000458}
459
Scott Michel266bc8f2007-12-04 22:23:35 +0000460/*!
461 \arg Op The ISD instruction (ignored)
462 \arg N The address to be tested
463 \arg Base Base address register/pointer
464 \arg Index Base address index
465
466 Examine the input address by a base register plus a signed 10-bit
467 displacement, [r+I10] (D-form address).
468
469 \return true if \a N is a D-form address with \a Base and \a Index set
Dan Gohman475871a2008-07-27 21:46:04 +0000470 to non-empty SDValue instances.
Scott Michel266bc8f2007-12-04 22:23:35 +0000471*/
472bool
Dan Gohman475871a2008-07-27 21:46:04 +0000473SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
474 SDValue &Index) {
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000475 return DFormAddressPredicate(Op, N, Base, Index,
Scott Michel9c0c6b22008-11-21 02:56:16 +0000476 SPUFrameInfo::minFrameOffset(),
477 SPUFrameInfo::maxFrameOffset());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000478}
479
480bool
Dan Gohman475871a2008-07-27 21:46:04 +0000481SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base,
482 SDValue &Index, int minOffset,
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000483 int maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000484 unsigned Opc = N.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000485 MVT PtrTy = SPUtli.getPointerTy();
Scott Michel266bc8f2007-12-04 22:23:35 +0000486
Scott Michel053c1da2008-01-29 02:16:57 +0000487 if (Opc == ISD::FrameIndex) {
488 // Stack frame index must be less than 512 (divided by 16):
Scott Michel203b2d62008-04-30 00:30:08 +0000489 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
490 int FI = int(FIN->getIndex());
Scott Michel266bc8f2007-12-04 22:23:35 +0000491 DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
Scott Michel203b2d62008-04-30 00:30:08 +0000492 << FI << "\n");
493 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000494 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000495 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel266bc8f2007-12-04 22:23:35 +0000496 return true;
497 }
498 } else if (Opc == ISD::ADD) {
499 // Generated by getelementptr
Dan Gohman475871a2008-07-27 21:46:04 +0000500 const SDValue Op0 = N.getOperand(0);
501 const SDValue Op1 = N.getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000502
Scott Michel053c1da2008-01-29 02:16:57 +0000503 if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
504 || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
505 Base = CurDAG->getTargetConstant(0, PtrTy);
506 Index = N;
507 return true;
508 } else if (Op1.getOpcode() == ISD::Constant
509 || Op1.getOpcode() == ISD::TargetConstant) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000510 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000511 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000512
Scott Michel053c1da2008-01-29 02:16:57 +0000513 if (Op0.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000514 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
515 int FI = int(FIN->getIndex());
Scott Michel9de5d0d2008-01-11 02:53:15 +0000516 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000517 << " frame index = " << FI << "\n");
Scott Michel9de5d0d2008-01-11 02:53:15 +0000518
Scott Michel203b2d62008-04-30 00:30:08 +0000519 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000520 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000521 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000522 return true;
523 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000524 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel9de5d0d2008-01-11 02:53:15 +0000525 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000526 Index = Op0;
527 return true;
528 }
529 } else if (Op0.getOpcode() == ISD::Constant
530 || Op0.getOpcode() == ISD::TargetConstant) {
531 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000532 int32_t offset = int32_t(CN->getSExtValue());
Scott Michel053c1da2008-01-29 02:16:57 +0000533
534 if (Op1.getOpcode() == ISD::FrameIndex) {
Scott Michel203b2d62008-04-30 00:30:08 +0000535 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
536 int FI = int(FIN->getIndex());
Scott Michel053c1da2008-01-29 02:16:57 +0000537 DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
Scott Michel203b2d62008-04-30 00:30:08 +0000538 << " frame index = " << FI << "\n");
Scott Michel053c1da2008-01-29 02:16:57 +0000539
Scott Michel203b2d62008-04-30 00:30:08 +0000540 if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000541 Base = CurDAG->getTargetConstant(offset, PtrTy);
Scott Michel203b2d62008-04-30 00:30:08 +0000542 Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
Scott Michel9de5d0d2008-01-11 02:53:15 +0000543 return true;
544 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000545 } else if (offset > minOffset && offset < maxOffset) {
Scott Michel053c1da2008-01-29 02:16:57 +0000546 Base = CurDAG->getTargetConstant(offset, PtrTy);
547 Index = Op1;
548 return true;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000549 }
Scott Michel053c1da2008-01-29 02:16:57 +0000550 }
551 } else if (Opc == SPUISD::IndirectAddr) {
552 // Indirect with constant offset -> D-Form address
Dan Gohman475871a2008-07-27 21:46:04 +0000553 const SDValue Op0 = N.getOperand(0);
554 const SDValue Op1 = N.getOperand(1);
Scott Michel497e8882008-01-11 21:01:19 +0000555
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000556 if (Op0.getOpcode() == SPUISD::Hi
557 && Op1.getOpcode() == SPUISD::Lo) {
Scott Michel053c1da2008-01-29 02:16:57 +0000558 // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
Scott Michel9de5d0d2008-01-11 02:53:15 +0000559 Base = CurDAG->getTargetConstant(0, PtrTy);
Scott Michel053c1da2008-01-29 02:16:57 +0000560 Index = N;
Scott Michel9de5d0d2008-01-11 02:53:15 +0000561 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000562 } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
563 int32_t offset = 0;
Dan Gohman475871a2008-07-27 21:46:04 +0000564 SDValue idxOp;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000565
566 if (isa<ConstantSDNode>(Op1)) {
567 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000568 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000569 idxOp = Op0;
570 } else if (isa<ConstantSDNode>(Op0)) {
571 ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
Dan Gohman7810bfe2008-09-26 21:54:37 +0000572 offset = int32_t(CN->getSExtValue());
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000573 idxOp = Op1;
Scott Michel02d711b2008-12-30 23:28:25 +0000574 }
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000575
576 if (offset >= minOffset && offset <= maxOffset) {
577 Base = CurDAG->getTargetConstant(offset, PtrTy);
578 Index = idxOp;
579 return true;
580 }
Scott Michel9de5d0d2008-01-11 02:53:15 +0000581 }
Scott Michel053c1da2008-01-29 02:16:57 +0000582 } else if (Opc == SPUISD::AFormAddr) {
583 Base = CurDAG->getTargetConstant(0, N.getValueType());
584 Index = N;
Scott Michel58c58182008-01-17 20:38:41 +0000585 return true;
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000586 } else if (Opc == SPUISD::LDRESULT) {
587 Base = CurDAG->getTargetConstant(0, N.getValueType());
588 Index = N;
589 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000590 } else if (Opc == ISD::Register || Opc == ISD::CopyFromReg) {
591 unsigned OpOpc = Op.getOpcode();
592
593 if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
594 // Direct load/store without getelementptr
595 SDValue Addr, Offs;
596
597 // Get the register from CopyFromReg
598 if (Opc == ISD::CopyFromReg)
599 Addr = N.getOperand(1);
600 else
601 Addr = N; // Register
602
Scott Michelaedc6372008-12-10 00:15:19 +0000603 Offs = ((OpOpc == ISD::STORE) ? Op.getOperand(3) : Op.getOperand(2));
Scott Michel9c0c6b22008-11-21 02:56:16 +0000604
605 if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
606 if (Offs.getOpcode() == ISD::UNDEF)
607 Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
608
609 Base = Offs;
610 Index = Addr;
611 return true;
612 }
Scott Michelaedc6372008-12-10 00:15:19 +0000613 } else {
614 /* If otherwise unadorned, default to D-form address with 0 offset: */
615 if (Opc == ISD::CopyFromReg) {
Scott Michel19c10e62009-01-26 03:37:41 +0000616 Index = N.getOperand(1);
Scott Michelaedc6372008-12-10 00:15:19 +0000617 } else {
Scott Michel19c10e62009-01-26 03:37:41 +0000618 Index = N;
Scott Michelaedc6372008-12-10 00:15:19 +0000619 }
620
621 Base = CurDAG->getTargetConstant(0, Index.getValueType());
622 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000623 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000624 }
Scott Michel9c0c6b22008-11-21 02:56:16 +0000625
Scott Michel266bc8f2007-12-04 22:23:35 +0000626 return false;
627}
628
629/*!
630 \arg Op The ISD instruction operand
631 \arg N The address operand
632 \arg Base The base pointer operand
633 \arg Index The offset/index operand
634
Scott Michel9c0c6b22008-11-21 02:56:16 +0000635 If the address \a N can be expressed as an A-form or D-form address, returns
636 false. Otherwise, creates two operands, Base and Index that will become the
637 (r)(r) X-form address.
Scott Michel266bc8f2007-12-04 22:23:35 +0000638*/
639bool
Dan Gohman475871a2008-07-27 21:46:04 +0000640SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
641 SDValue &Index) {
Scott Michel9c0c6b22008-11-21 02:56:16 +0000642 if (!SelectAFormAddr(Op, N, Base, Index)
643 && !SelectDFormAddr(Op, N, Base, Index)) {
Scott Michel18fae692008-11-25 17:29:43 +0000644 // If the address is neither A-form or D-form, punt and use an X-form
645 // address:
Scott Michel1a6cdb62008-12-01 17:56:02 +0000646 Base = N.getOperand(1);
647 Index = N.getOperand(0);
Scott Michel50843c02008-11-25 04:03:47 +0000648 return true;
Scott Michel9c0c6b22008-11-21 02:56:16 +0000649 }
650
651 return false;
Scott Michel58c58182008-01-17 20:38:41 +0000652}
653
Scott Michel266bc8f2007-12-04 22:23:35 +0000654//! Convert the operand from a target-independent to a target-specific node
655/*!
656 */
657SDNode *
Dan Gohman475871a2008-07-27 21:46:04 +0000658SPUDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000659 SDNode *N = Op.getNode();
Scott Michel266bc8f2007-12-04 22:23:35 +0000660 unsigned Opc = N->getOpcode();
Scott Michel58c58182008-01-17 20:38:41 +0000661 int n_ops = -1;
662 unsigned NewOpc;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000663 MVT OpVT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000664 SDValue Ops[8];
Dale Johannesened2eee62009-02-06 01:31:28 +0000665 DebugLoc dl = N->getDebugLoc();
Scott Michel266bc8f2007-12-04 22:23:35 +0000666
Dan Gohmane8be6c62008-07-17 19:10:17 +0000667 if (N->isMachineOpcode()) {
Scott Michel266bc8f2007-12-04 22:23:35 +0000668 return NULL; // Already selected.
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000669 }
670
671 if (Opc == ISD::FrameIndex) {
Scott Michel02d711b2008-12-30 23:28:25 +0000672 int FI = cast<FrameIndexSDNode>(N)->getIndex();
673 SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
674 SDValue Imm0 = CurDAG->getTargetConstant(0, Op.getValueType());
Scott Michel266bc8f2007-12-04 22:23:35 +0000675
Scott Michel02d711b2008-12-30 23:28:25 +0000676 if (FI < 128) {
Scott Michel203b2d62008-04-30 00:30:08 +0000677 NewOpc = SPU::AIr32;
Scott Michel02d711b2008-12-30 23:28:25 +0000678 Ops[0] = TFI;
679 Ops[1] = Imm0;
Scott Michel203b2d62008-04-30 00:30:08 +0000680 n_ops = 2;
681 } else {
Scott Michel203b2d62008-04-30 00:30:08 +0000682 NewOpc = SPU::Ar32;
Scott Michel02d711b2008-12-30 23:28:25 +0000683 Ops[0] = CurDAG->getRegister(SPU::R1, Op.getValueType());
Dale Johannesened2eee62009-02-06 01:31:28 +0000684 Ops[1] = SDValue(CurDAG->getTargetNode(SPU::ILAr32, dl, Op.getValueType(),
Scott Michel02d711b2008-12-30 23:28:25 +0000685 TFI, Imm0), 0);
Scott Michel203b2d62008-04-30 00:30:08 +0000686 n_ops = 2;
Scott Michel203b2d62008-04-30 00:30:08 +0000687 }
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000688 } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
689 // Catch the i64 constants that end up here. Note: The backend doesn't
690 // attempt to legalize the constant (it's useless because DAGCombiner
691 // will insert 64-bit constants and we can't stop it).
692 return SelectI64Constant(Op, OpVT);
Scott Michel94bd57e2009-01-15 04:41:47 +0000693 } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
694 && OpVT == MVT::i64) {
695 SDValue Op0 = Op.getOperand(0);
696 MVT Op0VT = Op0.getValueType();
697 MVT Op0VecVT = MVT::getVectorVT(Op0VT, (128 / Op0VT.getSizeInBits()));
698 MVT OpVecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
699 SDValue shufMask;
Scott Michel58c58182008-01-17 20:38:41 +0000700
Scott Michel94bd57e2009-01-15 04:41:47 +0000701 switch (Op0VT.getSimpleVT()) {
702 default:
703 cerr << "CellSPU Select: Unhandled zero/any extend MVT\n";
704 abort();
705 /*NOTREACHED*/
706 break;
707 case MVT::i32:
Evan Chenga87008d2009-02-25 22:49:59 +0000708 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000709 CurDAG->getConstant(0x80808080, MVT::i32),
710 CurDAG->getConstant(0x00010203, MVT::i32),
711 CurDAG->getConstant(0x80808080, MVT::i32),
712 CurDAG->getConstant(0x08090a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000713 break;
714
715 case MVT::i16:
Evan Chenga87008d2009-02-25 22:49:59 +0000716 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000717 CurDAG->getConstant(0x80808080, MVT::i32),
718 CurDAG->getConstant(0x80800203, MVT::i32),
719 CurDAG->getConstant(0x80808080, MVT::i32),
720 CurDAG->getConstant(0x80800a0b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000721 break;
722
723 case MVT::i8:
Evan Chenga87008d2009-02-25 22:49:59 +0000724 shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000725 CurDAG->getConstant(0x80808080, MVT::i32),
726 CurDAG->getConstant(0x80808003, MVT::i32),
727 CurDAG->getConstant(0x80808080, MVT::i32),
728 CurDAG->getConstant(0x8080800b, MVT::i32));
Scott Michel94bd57e2009-01-15 04:41:47 +0000729 break;
Scott Michel58c58182008-01-17 20:38:41 +0000730 }
Scott Michel94bd57e2009-01-15 04:41:47 +0000731
732 SDNode *shufMaskLoad = emitBuildVector(shufMask);
733 SDNode *PromoteScalar =
Dale Johannesened2eee62009-02-06 01:31:28 +0000734 SelectCode(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl, Op0VecVT, Op0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000735
736 SDValue zextShuffle =
Dale Johannesened2eee62009-02-06 01:31:28 +0000737 CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000738 SDValue(PromoteScalar, 0),
739 SDValue(PromoteScalar, 0),
740 SDValue(shufMaskLoad, 0));
Scott Michel94bd57e2009-01-15 04:41:47 +0000741
742 // N.B.: BIT_CONVERT replaces and updates the zextShuffle node, so we
743 // re-use it in the VEC2PREFSLOT selection without needing to explicitly
744 // call SelectCode (it's already done for us.)
Dale Johannesen04692802009-02-07 00:56:46 +0000745 SelectCode(CurDAG->getNode(ISD::BIT_CONVERT, dl, OpVecVT, zextShuffle));
Dale Johannesened2eee62009-02-06 01:31:28 +0000746 return SelectCode(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
Scott Michel94bd57e2009-01-15 04:41:47 +0000747 zextShuffle));
748 } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
749 SDNode *CGLoad =
Dale Johannesened2eee62009-02-06 01:31:28 +0000750 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG, dl));
Scott Michel94bd57e2009-01-15 04:41:47 +0000751
Dale Johannesened2eee62009-02-06 01:31:28 +0000752 return SelectCode(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
Scott Michel94bd57e2009-01-15 04:41:47 +0000753 Op.getOperand(0), Op.getOperand(1),
754 SDValue(CGLoad, 0)));
755 } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
756 SDNode *CGLoad =
Dale Johannesened2eee62009-02-06 01:31:28 +0000757 emitBuildVector(SPU::getBorrowGenerateShufMask(*CurDAG, dl));
Scott Michel94bd57e2009-01-15 04:41:47 +0000758
Dale Johannesened2eee62009-02-06 01:31:28 +0000759 return SelectCode(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
Scott Michel94bd57e2009-01-15 04:41:47 +0000760 Op.getOperand(0), Op.getOperand(1),
761 SDValue(CGLoad, 0)));
762 } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
763 SDNode *CGLoad =
Dale Johannesened2eee62009-02-06 01:31:28 +0000764 emitBuildVector(SPU::getCarryGenerateShufMask(*CurDAG, dl));
Scott Michel94bd57e2009-01-15 04:41:47 +0000765
Dale Johannesened2eee62009-02-06 01:31:28 +0000766 return SelectCode(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
Scott Michel94bd57e2009-01-15 04:41:47 +0000767 Op.getOperand(0), Op.getOperand(1),
768 SDValue(CGLoad, 0)));
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000769 } else if (Opc == ISD::TRUNCATE) {
770 SDValue Op0 = Op.getOperand(0);
771 if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
772 && OpVT == MVT::i32
773 && Op0.getValueType() == MVT::i64) {
Scott Michel9de57a92009-01-26 22:33:37 +0000774 // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
775 //
776 // Take advantage of the fact that the upper 32 bits are in the
777 // i32 preferred slot and avoid shuffle gymnastics:
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000778 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
779 if (CN != 0) {
780 unsigned shift_amt = unsigned(CN->getZExtValue());
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000781
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000782 if (shift_amt >= 32) {
783 SDNode *hi32 =
Dale Johannesened2eee62009-02-06 01:31:28 +0000784 CurDAG->getTargetNode(SPU::ORr32_r64, dl, OpVT,
785 Op0.getOperand(0));
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000786
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000787 shift_amt -= 32;
788 if (shift_amt > 0) {
789 // Take care of the additional shift, if present:
790 SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
791 unsigned Opc = SPU::ROTMAIr32_i32;
Scott Michel9de57a92009-01-26 22:33:37 +0000792
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000793 if (Op0.getOpcode() == ISD::SRL)
794 Opc = SPU::ROTMr32;
Scott Micheld1e8d9c2009-01-21 04:58:48 +0000795
Dale Johannesened2eee62009-02-06 01:31:28 +0000796 hi32 = CurDAG->getTargetNode(Opc, dl, OpVT, SDValue(hi32, 0),
797 shift);
Scott Michelc9c8b2a2009-01-26 03:31:40 +0000798 }
799
800 return hi32;
801 }
802 }
803 }
Scott Michel02d711b2008-12-30 23:28:25 +0000804 } else if (Opc == ISD::SHL) {
805 if (OpVT == MVT::i64) {
806 return SelectSHLi64(Op, OpVT);
807 }
808 } else if (Opc == ISD::SRL) {
809 if (OpVT == MVT::i64) {
810 return SelectSRLi64(Op, OpVT);
811 }
812 } else if (Opc == ISD::SRA) {
813 if (OpVT == MVT::i64) {
814 return SelectSRAi64(Op, OpVT);
815 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000816 } else if (Opc == SPUISD::LDRESULT) {
817 // Custom select instructions for LDRESULT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000818 MVT VT = N->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000819 SDValue Arg = N->getOperand(0);
820 SDValue Chain = N->getOperand(1);
Scott Michel266bc8f2007-12-04 22:23:35 +0000821 SDNode *Result;
Scott Michela59d4692008-02-23 18:41:37 +0000822 const valtype_map_s *vtm = getValueTypeMapEntry(VT);
823
824 if (vtm->ldresult_ins == 0) {
825 cerr << "LDRESULT for unsupported type: "
Duncan Sands83ec4b62008-06-06 12:08:01 +0000826 << VT.getMVTString()
Scott Michela59d4692008-02-23 18:41:37 +0000827 << "\n";
828 abort();
829 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000830
Scott Michela59d4692008-02-23 18:41:37 +0000831 Opc = vtm->ldresult_ins;
832 if (vtm->ldresult_imm) {
Dan Gohman475871a2008-07-27 21:46:04 +0000833 SDValue Zero = CurDAG->getTargetConstant(0, VT);
Scott Michel86c041f2007-12-20 00:44:13 +0000834
Dale Johannesened2eee62009-02-06 01:31:28 +0000835 Result = CurDAG->getTargetNode(Opc, dl, VT, MVT::Other, Arg, Zero, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000836 } else {
Dale Johannesened2eee62009-02-06 01:31:28 +0000837 Result = CurDAG->getTargetNode(Opc, dl, VT, MVT::Other, Arg, Arg, Chain);
Scott Michel86c041f2007-12-20 00:44:13 +0000838 }
839
Scott Michel266bc8f2007-12-04 22:23:35 +0000840 return Result;
Scott Michel053c1da2008-01-29 02:16:57 +0000841 } else if (Opc == SPUISD::IndirectAddr) {
Scott Michelf0569be2008-12-27 04:51:36 +0000842 // Look at the operands: SelectCode() will catch the cases that aren't
843 // specifically handled here.
844 //
845 // SPUInstrInfo catches the following patterns:
846 // (SPUindirect (SPUhi ...), (SPUlo ...))
847 // (SPUindirect $sp, imm)
848 MVT VT = Op.getValueType();
849 SDValue Op0 = N->getOperand(0);
850 SDValue Op1 = N->getOperand(1);
851 RegisterSDNode *RN;
Scott Michel58c58182008-01-17 20:38:41 +0000852
Scott Michelf0569be2008-12-27 04:51:36 +0000853 if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
854 || (Op0.getOpcode() == ISD::Register
855 && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
856 && RN->getReg() != SPU::R1))) {
857 NewOpc = SPU::Ar32;
Scott Michel58c58182008-01-17 20:38:41 +0000858 if (Op1.getOpcode() == ISD::Constant) {
859 ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
Scott Michelf0569be2008-12-27 04:51:36 +0000860 Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
Scott Michel7f9ba9b2008-01-30 02:55:46 +0000861 NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
Scott Michel58c58182008-01-17 20:38:41 +0000862 }
Scott Michelf0569be2008-12-27 04:51:36 +0000863 Ops[0] = Op0;
864 Ops[1] = Op1;
865 n_ops = 2;
Scott Michel58c58182008-01-17 20:38:41 +0000866 }
Scott Michel266bc8f2007-12-04 22:23:35 +0000867 }
Scott Michel02d711b2008-12-30 23:28:25 +0000868
Scott Michel58c58182008-01-17 20:38:41 +0000869 if (n_ops > 0) {
870 if (N->hasOneUse())
871 return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
872 else
Dale Johannesened2eee62009-02-06 01:31:28 +0000873 return CurDAG->getTargetNode(NewOpc, dl, OpVT, Ops, n_ops);
Scott Michel58c58182008-01-17 20:38:41 +0000874 } else
875 return SelectCode(Op);
Scott Michel266bc8f2007-12-04 22:23:35 +0000876}
877
Scott Michel02d711b2008-12-30 23:28:25 +0000878/*!
879 * Emit the instruction sequence for i64 left shifts. The basic algorithm
880 * is to fill the bottom two word slots with zeros so that zeros are shifted
881 * in as the entire quadword is shifted left.
882 *
883 * \note This code could also be used to implement v2i64 shl.
884 *
885 * @param Op The shl operand
886 * @param OpVT Op's machine value value type (doesn't need to be passed, but
887 * makes life easier.)
888 * @return The SDNode with the entire instruction sequence
889 */
890SDNode *
891SPUDAGToDAGISel::SelectSHLi64(SDValue &Op, MVT OpVT) {
892 SDValue Op0 = Op.getOperand(0);
893 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
894 SDValue ShiftAmt = Op.getOperand(1);
895 MVT ShiftAmtVT = ShiftAmt.getValueType();
896 SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
897 SDValue SelMaskVal;
Dale Johannesened2eee62009-02-06 01:31:28 +0000898 DebugLoc dl = Op.getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000899
Dale Johannesened2eee62009-02-06 01:31:28 +0000900 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
Scott Michel02d711b2008-12-30 23:28:25 +0000901 SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
Dale Johannesened2eee62009-02-06 01:31:28 +0000902 SelMask = CurDAG->getTargetNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
903 ZeroFill = CurDAG->getTargetNode(SPU::ILv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000904 CurDAG->getTargetConstant(0, OpVT));
Dale Johannesened2eee62009-02-06 01:31:28 +0000905 VecOp0 = CurDAG->getTargetNode(SPU::SELBv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000906 SDValue(ZeroFill, 0),
907 SDValue(VecOp0, 0),
908 SDValue(SelMask, 0));
909
910 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
911 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
912 unsigned bits = unsigned(CN->getZExtValue()) & 7;
913
914 if (bytes > 0) {
915 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +0000916 CurDAG->getTargetNode(SPU::SHLQBYIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000917 SDValue(VecOp0, 0),
918 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
919 }
920
921 if (bits > 0) {
922 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +0000923 CurDAG->getTargetNode(SPU::SHLQBIIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000924 SDValue((Shift != 0 ? Shift : VecOp0), 0),
925 CurDAG->getTargetConstant(bits, ShiftAmtVT));
926 }
927 } else {
928 SDNode *Bytes =
Dale Johannesened2eee62009-02-06 01:31:28 +0000929 CurDAG->getTargetNode(SPU::ROTMIr32, dl, ShiftAmtVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000930 ShiftAmt,
931 CurDAG->getTargetConstant(3, ShiftAmtVT));
932 SDNode *Bits =
Dale Johannesened2eee62009-02-06 01:31:28 +0000933 CurDAG->getTargetNode(SPU::ANDIr32, dl, ShiftAmtVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000934 ShiftAmt,
935 CurDAG->getTargetConstant(7, ShiftAmtVT));
936 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +0000937 CurDAG->getTargetNode(SPU::SHLQBYv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000938 SDValue(VecOp0, 0), SDValue(Bytes, 0));
939 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +0000940 CurDAG->getTargetNode(SPU::SHLQBIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000941 SDValue(Shift, 0), SDValue(Bits, 0));
942 }
943
Dale Johannesened2eee62009-02-06 01:31:28 +0000944 return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +0000945}
946
947/*!
948 * Emit the instruction sequence for i64 logical right shifts.
949 *
950 * @param Op The shl operand
951 * @param OpVT Op's machine value value type (doesn't need to be passed, but
952 * makes life easier.)
953 * @return The SDNode with the entire instruction sequence
954 */
955SDNode *
956SPUDAGToDAGISel::SelectSRLi64(SDValue &Op, MVT OpVT) {
957 SDValue Op0 = Op.getOperand(0);
958 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
959 SDValue ShiftAmt = Op.getOperand(1);
960 MVT ShiftAmtVT = ShiftAmt.getValueType();
961 SDNode *VecOp0, *Shift = 0;
Dale Johannesened2eee62009-02-06 01:31:28 +0000962 DebugLoc dl = Op.getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +0000963
Dale Johannesened2eee62009-02-06 01:31:28 +0000964 VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
Scott Michel02d711b2008-12-30 23:28:25 +0000965
966 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
967 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
968 unsigned bits = unsigned(CN->getZExtValue()) & 7;
969
970 if (bytes > 0) {
971 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +0000972 CurDAG->getTargetNode(SPU::ROTQMBYIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000973 SDValue(VecOp0, 0),
974 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
975 }
976
977 if (bits > 0) {
978 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +0000979 CurDAG->getTargetNode(SPU::ROTQMBIIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000980 SDValue((Shift != 0 ? Shift : VecOp0), 0),
981 CurDAG->getTargetConstant(bits, ShiftAmtVT));
982 }
983 } else {
984 SDNode *Bytes =
Dale Johannesened2eee62009-02-06 01:31:28 +0000985 CurDAG->getTargetNode(SPU::ROTMIr32, dl, ShiftAmtVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000986 ShiftAmt,
987 CurDAG->getTargetConstant(3, ShiftAmtVT));
988 SDNode *Bits =
Dale Johannesened2eee62009-02-06 01:31:28 +0000989 CurDAG->getTargetNode(SPU::ANDIr32, dl, ShiftAmtVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000990 ShiftAmt,
991 CurDAG->getTargetConstant(7, ShiftAmtVT));
992
993 // Ensure that the shift amounts are negated!
Dale Johannesened2eee62009-02-06 01:31:28 +0000994 Bytes = CurDAG->getTargetNode(SPU::SFIr32, dl, ShiftAmtVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000995 SDValue(Bytes, 0),
996 CurDAG->getTargetConstant(0, ShiftAmtVT));
997
Dale Johannesened2eee62009-02-06 01:31:28 +0000998 Bits = CurDAG->getTargetNode(SPU::SFIr32, dl, ShiftAmtVT,
Scott Michel02d711b2008-12-30 23:28:25 +0000999 SDValue(Bits, 0),
1000 CurDAG->getTargetConstant(0, ShiftAmtVT));
1001
1002 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +00001003 CurDAG->getTargetNode(SPU::ROTQMBYv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001004 SDValue(VecOp0, 0), SDValue(Bytes, 0));
1005 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +00001006 CurDAG->getTargetNode(SPU::ROTQMBIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001007 SDValue(Shift, 0), SDValue(Bits, 0));
1008 }
1009
Dale Johannesened2eee62009-02-06 01:31:28 +00001010 return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001011}
1012
1013/*!
1014 * Emit the instruction sequence for i64 arithmetic right shifts.
1015 *
1016 * @param Op The shl operand
1017 * @param OpVT Op's machine value value type (doesn't need to be passed, but
1018 * makes life easier.)
1019 * @return The SDNode with the entire instruction sequence
1020 */
1021SDNode *
1022SPUDAGToDAGISel::SelectSRAi64(SDValue &Op, MVT OpVT) {
1023 // Promote Op0 to vector
1024 MVT VecVT = MVT::getVectorVT(OpVT, (128 / OpVT.getSizeInBits()));
1025 SDValue ShiftAmt = Op.getOperand(1);
1026 MVT ShiftAmtVT = ShiftAmt.getValueType();
Dale Johannesened2eee62009-02-06 01:31:28 +00001027 DebugLoc dl = Op.getDebugLoc();
Scott Michel02d711b2008-12-30 23:28:25 +00001028
1029 SDNode *VecOp0 =
Dale Johannesened2eee62009-02-06 01:31:28 +00001030 CurDAG->getTargetNode(SPU::ORv2i64_i64, dl, VecVT, Op.getOperand(0));
Scott Michel02d711b2008-12-30 23:28:25 +00001031
1032 SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1033 SDNode *SignRot =
Dale Johannesened2eee62009-02-06 01:31:28 +00001034 CurDAG->getTargetNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
Scott Michel02d711b2008-12-30 23:28:25 +00001035 SDValue(VecOp0, 0), SignRotAmt);
1036 SDNode *UpperHalfSign =
Dale Johannesened2eee62009-02-06 01:31:28 +00001037 CurDAG->getTargetNode(SPU::ORi32_v4i32, dl, MVT::i32, SDValue(SignRot, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001038
1039 SDNode *UpperHalfSignMask =
Dale Johannesened2eee62009-02-06 01:31:28 +00001040 CurDAG->getTargetNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001041 SDNode *UpperLowerMask =
Dale Johannesened2eee62009-02-06 01:31:28 +00001042 CurDAG->getTargetNode(SPU::FSMBIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001043 CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
1044 SDNode *UpperLowerSelect =
Dale Johannesened2eee62009-02-06 01:31:28 +00001045 CurDAG->getTargetNode(SPU::SELBv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001046 SDValue(UpperHalfSignMask, 0),
1047 SDValue(VecOp0, 0),
1048 SDValue(UpperLowerMask, 0));
1049
1050 SDNode *Shift = 0;
1051
1052 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1053 unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1054 unsigned bits = unsigned(CN->getZExtValue()) & 7;
1055
1056 if (bytes > 0) {
1057 bytes = 31 - bytes;
1058 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +00001059 CurDAG->getTargetNode(SPU::ROTQBYIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001060 SDValue(UpperLowerSelect, 0),
1061 CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1062 }
1063
1064 if (bits > 0) {
1065 bits = 8 - bits;
1066 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +00001067 CurDAG->getTargetNode(SPU::ROTQBIIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001068 SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1069 CurDAG->getTargetConstant(bits, ShiftAmtVT));
1070 }
1071 } else {
1072 SDNode *NegShift =
Dale Johannesened2eee62009-02-06 01:31:28 +00001073 CurDAG->getTargetNode(SPU::SFIr32, dl, ShiftAmtVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001074 ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
1075
1076 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +00001077 CurDAG->getTargetNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001078 SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
1079 Shift =
Dale Johannesened2eee62009-02-06 01:31:28 +00001080 CurDAG->getTargetNode(SPU::ROTQBIv2i64, dl, VecVT,
Scott Michel02d711b2008-12-30 23:28:25 +00001081 SDValue(Shift, 0), SDValue(NegShift, 0));
1082 }
1083
Dale Johannesened2eee62009-02-06 01:31:28 +00001084 return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
Scott Michel02d711b2008-12-30 23:28:25 +00001085}
1086
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001087/*!
1088 Do the necessary magic necessary to load a i64 constant
1089 */
1090SDNode *SPUDAGToDAGISel::SelectI64Constant(SDValue& Op, MVT OpVT) {
1091 ConstantSDNode *CN = cast<ConstantSDNode>(Op.getNode());
Dale Johannesened2eee62009-02-06 01:31:28 +00001092 // Currently there's no DL on the input, but won't hurt to pretend.
1093 DebugLoc dl = Op.getDebugLoc();
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001094 MVT OpVecVT = MVT::getVectorVT(OpVT, 2);
1095 SDValue i64vec =
Dale Johannesened2eee62009-02-06 01:31:28 +00001096 SPU::LowerSplat_v2i64(OpVecVT, *CurDAG, CN->getZExtValue(), dl);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001097
1098 // Here's where it gets interesting, because we have to parse out the
1099 // subtree handed back in i64vec:
1100
1101 if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
1102 // The degenerate case where the upper and lower bits in the splat are
1103 // identical:
1104 SDValue Op0 = i64vec.getOperand(0);
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001105
Scott Michel9de57a92009-01-26 22:33:37 +00001106 ReplaceUses(i64vec, Op0);
Dale Johannesened2eee62009-02-06 01:31:28 +00001107 return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001108 SDValue(emitBuildVector(Op0), 0));
1109 } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1110 SDValue lhs = i64vec.getOperand(0);
1111 SDValue rhs = i64vec.getOperand(1);
1112 SDValue shufmask = i64vec.getOperand(2);
1113
1114 if (lhs.getOpcode() == ISD::BIT_CONVERT) {
1115 ReplaceUses(lhs, lhs.getOperand(0));
1116 lhs = lhs.getOperand(0);
1117 }
1118
1119 SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1120 ? lhs.getNode()
1121 : emitBuildVector(lhs));
1122
1123 if (rhs.getOpcode() == ISD::BIT_CONVERT) {
1124 ReplaceUses(rhs, rhs.getOperand(0));
1125 rhs = rhs.getOperand(0);
1126 }
1127
1128 SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1129 ? rhs.getNode()
1130 : emitBuildVector(rhs));
Scott Michel9de57a92009-01-26 22:33:37 +00001131
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001132 if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
1133 ReplaceUses(shufmask, shufmask.getOperand(0));
1134 shufmask = shufmask.getOperand(0);
1135 }
1136
1137 SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1138 ? shufmask.getNode()
1139 : emitBuildVector(shufmask));
1140
1141 SDNode *shufNode =
Dale Johannesened2eee62009-02-06 01:31:28 +00001142 Select(CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001143 SDValue(lhsNode, 0), SDValue(rhsNode, 0),
1144 SDValue(shufMaskNode, 0)));
1145
Dale Johannesened2eee62009-02-06 01:31:28 +00001146 return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT,
1147 SDValue(shufNode, 0));
Scott Michelc9c8b2a2009-01-26 03:31:40 +00001148 } else {
1149 cerr << "SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec condition\n";
1150 abort();
1151 }
1152}
1153
Scott Michel02d711b2008-12-30 23:28:25 +00001154/// createSPUISelDag - This pass converts a legalized DAG into a
Scott Michel266bc8f2007-12-04 22:23:35 +00001155/// SPU-specific DAG, ready for instruction scheduling.
1156///
1157FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1158 return new SPUDAGToDAGISel(TM);
1159}