Null initialize a few variables flagged by
[oota-llvm.git] / lib / Target / CellSPU / SPUISelDAGToDAG.cpp
1 //===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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 "SPUHazardRecognizers.h"
18 #include "SPUFrameLowering.h"
19 #include "SPURegisterNames.h"
20 #include "SPUTargetMachine.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/CodeGen/PseudoSourceValue.h"
27 #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"
32 #include "llvm/LLVMContext.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/MathExtras.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/raw_ostream.h"
38
39 using namespace llvm;
40
41 namespace {
42   //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
43   bool
44   isI32IntS10Immediate(ConstantSDNode *CN)
45   {
46     return isInt<10>(CN->getSExtValue());
47   }
48
49   //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
50   bool
51   isI32IntU10Immediate(ConstantSDNode *CN)
52   {
53     return isUInt<10>(CN->getSExtValue());
54   }
55
56   //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
57   bool
58   isI16IntS10Immediate(ConstantSDNode *CN)
59   {
60     return isInt<10>(CN->getSExtValue());
61   }
62
63   //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
64   bool
65   isI16IntU10Immediate(ConstantSDNode *CN)
66   {
67     return isUInt<10>((short) CN->getZExtValue());
68   }
69
70   //! ConstantSDNode predicate for signed 16-bit values
71   /*!
72     \arg CN The constant SelectionDAG node holding the value
73     \arg Imm The returned 16-bit value, if returning true
74
75     This predicate tests the value in \a CN to see whether it can be
76     represented as a 16-bit, sign-extended quantity. Returns true if
77     this is the case.
78    */
79   bool
80   isIntS16Immediate(ConstantSDNode *CN, short &Imm)
81   {
82     EVT vt = CN->getValueType(0);
83     Imm = (short) CN->getZExtValue();
84     if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
85       return true;
86     } else if (vt == MVT::i32) {
87       int32_t i_val = (int32_t) CN->getZExtValue();
88       short s_val = (short) i_val;
89       return i_val == s_val;
90     } else {
91       int64_t i_val = (int64_t) CN->getZExtValue();
92       short s_val = (short) i_val;
93       return i_val == s_val;
94     }
95
96     return false;
97   }
98
99   //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
100   static bool
101   isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
102   {
103     EVT vt = FPN->getValueType(0);
104     if (vt == MVT::f32) {
105       int val = FloatToBits(FPN->getValueAPF().convertToFloat());
106       int sval = (int) ((val << 16) >> 16);
107       Imm = (short) val;
108       return val == sval;
109     }
110
111     return false;
112   }
113
114   //! Generate the carry-generate shuffle mask.
115   SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
116     SmallVector<SDValue, 16 > ShufBytes;
117
118     // Create the shuffle mask for "rotating" the borrow up one register slot
119     // once the borrow is generated.
120     ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
121     ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
122     ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
123     ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
124
125     return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
126                        &ShufBytes[0], ShufBytes.size());
127   }
128
129   //! Generate the borrow-generate shuffle mask
130   SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
131     SmallVector<SDValue, 16 > ShufBytes;
132
133     // Create the shuffle mask for "rotating" the borrow up one register slot
134     // once the borrow is generated.
135     ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
136     ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
137     ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
138     ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
139
140     return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
141                        &ShufBytes[0], ShufBytes.size());
142   }
143
144   //===------------------------------------------------------------------===//
145   /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
146   /// instructions for SelectionDAG operations.
147   ///
148   class SPUDAGToDAGISel :
149     public SelectionDAGISel
150   {
151     const SPUTargetMachine &TM;
152     const SPUTargetLowering &SPUtli;
153     unsigned GlobalBaseReg;
154
155   public:
156     explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
157       SelectionDAGISel(tm),
158       TM(tm),
159       SPUtli(*tm.getTargetLowering())
160     { }
161
162     virtual bool runOnMachineFunction(MachineFunction &MF) {
163       // Make sure we re-emit a set of the global base reg if necessary
164       GlobalBaseReg = 0;
165       SelectionDAGISel::runOnMachineFunction(MF);
166       return true;
167     }
168
169     /// getI32Imm - Return a target constant with the specified value, of type
170     /// i32.
171     inline SDValue getI32Imm(uint32_t Imm) {
172       return CurDAG->getTargetConstant(Imm, MVT::i32);
173     }
174
175     /// getSmallIPtrImm - Return a target constant of pointer type.
176     inline SDValue getSmallIPtrImm(unsigned Imm) {
177       return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
178     }
179
180     SDNode *emitBuildVector(SDNode *bvNode) {
181       EVT vecVT = bvNode->getValueType(0);
182       DebugLoc dl = bvNode->getDebugLoc();
183
184       // Check to see if this vector can be represented as a CellSPU immediate
185       // constant by invoking all of the instruction selection predicates:
186       if (((vecVT == MVT::v8i16) &&
187            (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
188           ((vecVT == MVT::v4i32) &&
189            ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
190             (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
191             (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
192             (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
193           ((vecVT == MVT::v2i64) &&
194            ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
195             (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
196             (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0)))) {
197         HandleSDNode Dummy(SDValue(bvNode, 0));
198         if (SDNode *N = Select(bvNode))
199           return N;
200         return Dummy.getValue().getNode();
201       }
202
203       // No, need to emit a constant pool spill:
204       std::vector<Constant*> CV;
205
206       for (size_t i = 0; i < bvNode->getNumOperands(); ++i) {
207         ConstantSDNode *V = cast<ConstantSDNode > (bvNode->getOperand(i));
208         CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
209       }
210
211       const Constant *CP = ConstantVector::get(CV);
212       SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
213       unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
214       SDValue CGPoolOffset =
215               SPU::LowerConstantPool(CPIdx, *CurDAG, TM);
216
217       HandleSDNode Dummy(CurDAG->getLoad(vecVT, dl,
218                                          CurDAG->getEntryNode(), CGPoolOffset,
219                                          MachinePointerInfo::getConstantPool(),
220                                          false, false, Alignment));
221       CurDAG->ReplaceAllUsesWith(SDValue(bvNode, 0), Dummy.getValue());
222       if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
223         return N;
224       return Dummy.getValue().getNode();
225     }
226
227     /// Select - Convert the specified operand from a target-independent to a
228     /// target-specific node if it hasn't already been changed.
229     SDNode *Select(SDNode *N);
230
231     //! Emit the instruction sequence for i64 shl
232     SDNode *SelectSHLi64(SDNode *N, EVT OpVT);
233
234     //! Emit the instruction sequence for i64 srl
235     SDNode *SelectSRLi64(SDNode *N, EVT OpVT);
236
237     //! Emit the instruction sequence for i64 sra
238     SDNode *SelectSRAi64(SDNode *N, EVT OpVT);
239
240     //! Emit the necessary sequence for loading i64 constants:
241     SDNode *SelectI64Constant(SDNode *N, EVT OpVT, DebugLoc dl);
242
243     //! Alternate instruction emit sequence for loading i64 constants
244     SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
245
246     //! Returns true if the address N is an A-form (local store) address
247     bool SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
248                          SDValue &Index);
249
250     //! D-form address predicate
251     bool SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
252                          SDValue &Index);
253
254     /// Alternate D-form address using i7 offset predicate
255     bool SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
256                           SDValue &Base);
257
258     /// D-form address selection workhorse
259     bool DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Disp,
260                                SDValue &Base, int minOffset, int maxOffset);
261
262     //! Address predicate if N can be expressed as an indexed [r+r] operation.
263     bool SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
264                          SDValue &Index);
265
266     /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
267     /// inline asm expressions.
268     virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
269                                               char ConstraintCode,
270                                               std::vector<SDValue> &OutOps) {
271       SDValue Op0, Op1;
272       switch (ConstraintCode) {
273       default: return true;
274       case 'm':   // memory
275         if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
276             && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1))
277           SelectXFormAddr(Op.getNode(), Op, Op0, Op1);
278         break;
279       case 'o':   // offsetable
280         if (!SelectDFormAddr(Op.getNode(), Op, Op0, Op1)
281             && !SelectAFormAddr(Op.getNode(), Op, Op0, Op1)) {
282           Op0 = Op;
283           Op1 = getSmallIPtrImm(0);
284         }
285         break;
286       case 'v':   // not offsetable
287 #if 1
288         llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
289 #else
290         SelectAddrIdxOnly(Op, Op, Op0, Op1);
291 #endif
292         break;
293       }
294
295       OutOps.push_back(Op0);
296       OutOps.push_back(Op1);
297       return false;
298     }
299
300     virtual const char *getPassName() const {
301       return "Cell SPU DAG->DAG Pattern Instruction Selection";
302     }
303
304   private:
305     SDValue getRC( MVT );
306
307     // Include the pieces autogenerated from the target description.
308 #include "SPUGenDAGISel.inc"
309   };
310 }
311
312 /*!
313  \arg Op The ISD instruction operand
314  \arg N The address to be tested
315  \arg Base The base address
316  \arg Index The base address index
317  */
318 bool
319 SPUDAGToDAGISel::SelectAFormAddr(SDNode *Op, SDValue N, SDValue &Base,
320                     SDValue &Index) {
321   // These match the addr256k operand type:
322   EVT OffsVT = MVT::i16;
323   SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
324
325   switch (N.getOpcode()) {
326   case ISD::Constant:
327   case ISD::ConstantPool:
328   case ISD::GlobalAddress:
329     report_fatal_error("SPU SelectAFormAddr: Constant/Pool/Global not lowered.");
330     /*NOTREACHED*/
331
332   case ISD::TargetConstant:
333   case ISD::TargetGlobalAddress:
334   case ISD::TargetJumpTable:
335     report_fatal_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
336                       "not wrapped as A-form address.");
337     /*NOTREACHED*/
338
339   case SPUISD::AFormAddr:
340     // Just load from memory if there's only a single use of the location,
341     // otherwise, this will get handled below with D-form offset addresses
342     if (N.hasOneUse()) {
343       SDValue Op0 = N.getOperand(0);
344       switch (Op0.getOpcode()) {
345       case ISD::TargetConstantPool:
346       case ISD::TargetJumpTable:
347         Base = Op0;
348         Index = Zero;
349         return true;
350
351       case ISD::TargetGlobalAddress: {
352         GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
353         const GlobalValue *GV = GSDN->getGlobal();
354         if (GV->getAlignment() == 16) {
355           Base = Op0;
356           Index = Zero;
357           return true;
358         }
359         break;
360       }
361       }
362     }
363     break;
364   }
365   return false;
366 }
367
368 bool
369 SPUDAGToDAGISel::SelectDForm2Addr(SDNode *Op, SDValue N, SDValue &Disp,
370                                   SDValue &Base) {
371   const int minDForm2Offset = -(1 << 7);
372   const int maxDForm2Offset = (1 << 7) - 1;
373   return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
374                                maxDForm2Offset);
375 }
376
377 /*!
378   \arg Op The ISD instruction (ignored)
379   \arg N The address to be tested
380   \arg Base Base address register/pointer
381   \arg Index Base address index
382
383   Examine the input address by a base register plus a signed 10-bit
384   displacement, [r+I10] (D-form address).
385
386   \return true if \a N is a D-form address with \a Base and \a Index set
387   to non-empty SDValue instances.
388 */
389 bool
390 SPUDAGToDAGISel::SelectDFormAddr(SDNode *Op, SDValue N, SDValue &Base,
391                                  SDValue &Index) {
392   return DFormAddressPredicate(Op, N, Base, Index,
393                                SPUFrameLowering::minFrameOffset(),
394                                SPUFrameLowering::maxFrameOffset());
395 }
396
397 bool
398 SPUDAGToDAGISel::DFormAddressPredicate(SDNode *Op, SDValue N, SDValue &Base,
399                                       SDValue &Index, int minOffset,
400                                       int maxOffset) {
401   unsigned Opc = N.getOpcode();
402   EVT PtrTy = SPUtli.getPointerTy();
403
404   if (Opc == ISD::FrameIndex) {
405     // Stack frame index must be less than 512 (divided by 16):
406     FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(N);
407     int FI = int(FIN->getIndex());
408     DEBUG(errs() << "SelectDFormAddr: ISD::FrameIndex = "
409                << FI << "\n");
410     if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
411       Base = CurDAG->getTargetConstant(0, PtrTy);
412       Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
413       return true;
414     }
415   } else if (Opc == ISD::ADD) {
416     // Generated by getelementptr
417     const SDValue Op0 = N.getOperand(0);
418     const SDValue Op1 = N.getOperand(1);
419
420     if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
421         || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
422       Base = CurDAG->getTargetConstant(0, PtrTy);
423       Index = N;
424       return true;
425     } else if (Op1.getOpcode() == ISD::Constant
426                || Op1.getOpcode() == ISD::TargetConstant) {
427       ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
428       int32_t offset = int32_t(CN->getSExtValue());
429
430       if (Op0.getOpcode() == ISD::FrameIndex) {
431         FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op0);
432         int FI = int(FIN->getIndex());
433         DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
434                    << " frame index = " << FI << "\n");
435
436         if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
437           Base = CurDAG->getTargetConstant(offset, PtrTy);
438           Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
439           return true;
440         }
441       } else if (offset > minOffset && offset < maxOffset) {
442         Base = CurDAG->getTargetConstant(offset, PtrTy);
443         Index = Op0;
444         return true;
445       }
446     } else if (Op0.getOpcode() == ISD::Constant
447                || Op0.getOpcode() == ISD::TargetConstant) {
448       ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
449       int32_t offset = int32_t(CN->getSExtValue());
450
451       if (Op1.getOpcode() == ISD::FrameIndex) {
452         FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op1);
453         int FI = int(FIN->getIndex());
454         DEBUG(errs() << "SelectDFormAddr: ISD::ADD offset = " << offset
455                    << " frame index = " << FI << "\n");
456
457         if (SPUFrameLowering::FItoStackOffset(FI) < maxOffset) {
458           Base = CurDAG->getTargetConstant(offset, PtrTy);
459           Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
460           return true;
461         }
462       } else if (offset > minOffset && offset < maxOffset) {
463         Base = CurDAG->getTargetConstant(offset, PtrTy);
464         Index = Op1;
465         return true;
466       }
467     }
468   } else if (Opc == SPUISD::IndirectAddr) {
469     // Indirect with constant offset -> D-Form address
470     const SDValue Op0 = N.getOperand(0);
471     const SDValue Op1 = N.getOperand(1);
472
473     if (Op0.getOpcode() == SPUISD::Hi
474         && Op1.getOpcode() == SPUISD::Lo) {
475       // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
476       Base = CurDAG->getTargetConstant(0, PtrTy);
477       Index = N;
478       return true;
479     } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
480       int32_t offset = 0;
481       SDValue idxOp;
482
483       if (isa<ConstantSDNode>(Op1)) {
484         ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
485         offset = int32_t(CN->getSExtValue());
486         idxOp = Op0;
487       } else if (isa<ConstantSDNode>(Op0)) {
488         ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
489         offset = int32_t(CN->getSExtValue());
490         idxOp = Op1;
491       }
492
493       if (offset >= minOffset && offset <= maxOffset) {
494         Base = CurDAG->getTargetConstant(offset, PtrTy);
495         Index = idxOp;
496         return true;
497       }
498     }
499   } else if (Opc == SPUISD::AFormAddr) {
500     Base = CurDAG->getTargetConstant(0, N.getValueType());
501     Index = N;
502     return true;
503   } else if (Opc == SPUISD::LDRESULT) {
504     Base = CurDAG->getTargetConstant(0, N.getValueType());
505     Index = N;
506     return true;
507   } else if (Opc == ISD::Register
508            ||Opc == ISD::CopyFromReg
509            ||Opc == ISD::UNDEF
510            ||Opc == ISD::Constant) {
511     unsigned OpOpc = Op->getOpcode();
512
513     if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
514       // Direct load/store without getelementptr
515       SDValue Offs;
516
517       Offs = ((OpOpc == ISD::STORE) ? Op->getOperand(3) : Op->getOperand(2));
518
519       if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
520         if (Offs.getOpcode() == ISD::UNDEF)
521           Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
522
523         Base = Offs;
524         Index = N;
525         return true;
526       }
527     } else {
528       /* If otherwise unadorned, default to D-form address with 0 offset: */
529       if (Opc == ISD::CopyFromReg) {
530         Index = N.getOperand(1);
531       } else {
532         Index = N;
533       }
534
535       Base = CurDAG->getTargetConstant(0, Index.getValueType());
536       return true;
537     }
538   }
539
540   return false;
541 }
542
543 /*!
544   \arg Op The ISD instruction operand
545   \arg N The address operand
546   \arg Base The base pointer operand
547   \arg Index The offset/index operand
548
549   If the address \a N can be expressed as an A-form or D-form address, returns
550   false.  Otherwise, creates two operands, Base and Index that will become the
551   (r)(r) X-form address.
552 */
553 bool
554 SPUDAGToDAGISel::SelectXFormAddr(SDNode *Op, SDValue N, SDValue &Base,
555                                  SDValue &Index) {
556   if (!SelectAFormAddr(Op, N, Base, Index)
557       && !SelectDFormAddr(Op, N, Base, Index)) {
558     // If the address is neither A-form or D-form, punt and use an X-form
559     // address:
560     Base = N.getOperand(1);
561     Index = N.getOperand(0);
562     return true;
563   }
564
565   return false;
566 }
567
568 /*!
569  Utility function to use with COPY_TO_REGCLASS instructions. Returns a SDValue
570  to be used as the last parameter of a
571 CurDAG->getMachineNode(COPY_TO_REGCLASS,..., ) function call
572  \arg VT the value type for which we want a register class
573 */
574 SDValue SPUDAGToDAGISel::getRC( MVT VT ) {
575   switch( VT.SimpleTy ) {
576   case MVT::i8:
577     return CurDAG->getTargetConstant(SPU::R8CRegClass.getID(), MVT::i32);
578     break;
579   case MVT::i16:
580     return CurDAG->getTargetConstant(SPU::R16CRegClass.getID(), MVT::i32);
581     break;
582   case MVT::i32:
583     return CurDAG->getTargetConstant(SPU::R32CRegClass.getID(), MVT::i32);
584     break;
585   case MVT::f32:
586     return CurDAG->getTargetConstant(SPU::R32FPRegClass.getID(), MVT::i32);
587     break;
588   case MVT::i64:
589     return CurDAG->getTargetConstant(SPU::R64CRegClass.getID(), MVT::i32);
590     break;
591   case MVT::i128:
592     return CurDAG->getTargetConstant(SPU::GPRCRegClass.getID(), MVT::i32);
593     break;
594   case MVT::v16i8:
595   case MVT::v8i16:
596   case MVT::v4i32:
597   case MVT::v4f32:
598   case MVT::v2i64:
599   case MVT::v2f64:
600     return CurDAG->getTargetConstant(SPU::VECREGRegClass.getID(), MVT::i32);
601     break;
602   default:
603     assert( false && "add a new case here" );
604   }
605   return SDValue();
606 }
607
608 //! Convert the operand from a target-independent to a target-specific node
609 /*!
610  */
611 SDNode *
612 SPUDAGToDAGISel::Select(SDNode *N) {
613   unsigned Opc = N->getOpcode();
614   int n_ops = -1;
615   unsigned NewOpc = 0;
616   EVT OpVT = N->getValueType(0);
617   SDValue Ops[8];
618   DebugLoc dl = N->getDebugLoc();
619
620   if (N->isMachineOpcode())
621     return NULL;   // Already selected.
622
623   if (Opc == ISD::FrameIndex) {
624     int FI = cast<FrameIndexSDNode>(N)->getIndex();
625     SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
626     SDValue Imm0 = CurDAG->getTargetConstant(0, N->getValueType(0));
627
628     if (FI < 128) {
629       NewOpc = SPU::AIr32;
630       Ops[0] = TFI;
631       Ops[1] = Imm0;
632       n_ops = 2;
633     } else {
634       NewOpc = SPU::Ar32;
635       Ops[0] = CurDAG->getRegister(SPU::R1, N->getValueType(0));
636       Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILAr32, dl,
637                                               N->getValueType(0), TFI),
638                        0);
639       n_ops = 2;
640     }
641   } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
642     // Catch the i64 constants that end up here. Note: The backend doesn't
643     // attempt to legalize the constant (it's useless because DAGCombiner
644     // will insert 64-bit constants and we can't stop it).
645     return SelectI64Constant(N, OpVT, N->getDebugLoc());
646   } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
647              && OpVT == MVT::i64) {
648     SDValue Op0 = N->getOperand(0);
649     EVT Op0VT = Op0.getValueType();
650     EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
651                                     Op0VT, (128 / Op0VT.getSizeInBits()));
652     EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(),
653                                    OpVT, (128 / OpVT.getSizeInBits()));
654     SDValue shufMask;
655
656     switch (Op0VT.getSimpleVT().SimpleTy) {
657     default:
658       report_fatal_error("CellSPU Select: Unhandled zero/any extend EVT");
659       /*NOTREACHED*/
660     case MVT::i32:
661       shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
662                                  CurDAG->getConstant(0x80808080, MVT::i32),
663                                  CurDAG->getConstant(0x00010203, MVT::i32),
664                                  CurDAG->getConstant(0x80808080, MVT::i32),
665                                  CurDAG->getConstant(0x08090a0b, MVT::i32));
666       break;
667
668     case MVT::i16:
669       shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
670                                  CurDAG->getConstant(0x80808080, MVT::i32),
671                                  CurDAG->getConstant(0x80800203, MVT::i32),
672                                  CurDAG->getConstant(0x80808080, MVT::i32),
673                                  CurDAG->getConstant(0x80800a0b, MVT::i32));
674       break;
675
676     case MVT::i8:
677       shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
678                                  CurDAG->getConstant(0x80808080, MVT::i32),
679                                  CurDAG->getConstant(0x80808003, MVT::i32),
680                                  CurDAG->getConstant(0x80808080, MVT::i32),
681                                  CurDAG->getConstant(0x8080800b, MVT::i32));
682       break;
683     }
684
685     SDNode *shufMaskLoad = emitBuildVector(shufMask.getNode());
686
687     HandleSDNode PromoteScalar(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl,
688                                                Op0VecVT, Op0));
689
690     SDValue PromScalar;
691     if (SDNode *N = SelectCode(PromoteScalar.getValue().getNode()))
692       PromScalar = SDValue(N, 0);
693     else
694       PromScalar = PromoteScalar.getValue();
695
696     SDValue zextShuffle =
697             CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
698                             PromScalar, PromScalar,
699                             SDValue(shufMaskLoad, 0));
700
701     HandleSDNode Dummy2(zextShuffle);
702     if (SDNode *N = SelectCode(Dummy2.getValue().getNode()))
703       zextShuffle = SDValue(N, 0);
704     else
705       zextShuffle = Dummy2.getValue();
706     HandleSDNode Dummy(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
707                                        zextShuffle));
708
709     CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
710     SelectCode(Dummy.getValue().getNode());
711     return Dummy.getValue().getNode();
712   } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
713     SDNode *CGLoad =
714             emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
715
716     HandleSDNode Dummy(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
717                                        N->getOperand(0), N->getOperand(1),
718                                        SDValue(CGLoad, 0)));
719
720     CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
721     if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
722       return N;
723     return Dummy.getValue().getNode();
724   } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
725     SDNode *CGLoad =
726             emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl).getNode());
727
728     HandleSDNode Dummy(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
729                                        N->getOperand(0), N->getOperand(1),
730                                        SDValue(CGLoad, 0)));
731
732     CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
733     if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
734       return N;
735     return Dummy.getValue().getNode();
736   } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
737     SDNode *CGLoad =
738             emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl).getNode());
739
740     HandleSDNode Dummy(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
741                                        N->getOperand(0), N->getOperand(1),
742                                        SDValue(CGLoad, 0)));
743     CurDAG->ReplaceAllUsesWith(N, Dummy.getValue().getNode());
744     if (SDNode *N = SelectCode(Dummy.getValue().getNode()))
745       return N;
746     return Dummy.getValue().getNode();
747   } else if (Opc == ISD::TRUNCATE) {
748     SDValue Op0 = N->getOperand(0);
749     if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
750         && OpVT == MVT::i32
751         && Op0.getValueType() == MVT::i64) {
752       // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
753       //
754       // Take advantage of the fact that the upper 32 bits are in the
755       // i32 preferred slot and avoid shuffle gymnastics:
756       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
757       if (CN != 0) {
758         unsigned shift_amt = unsigned(CN->getZExtValue());
759
760         if (shift_amt >= 32) {
761           SDNode *hi32 =
762                   CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
763                                          Op0.getOperand(0), getRC(MVT::i32));
764
765           shift_amt -= 32;
766           if (shift_amt > 0) {
767             // Take care of the additional shift, if present:
768             SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
769             unsigned Opc = SPU::ROTMAIr32_i32;
770
771             if (Op0.getOpcode() == ISD::SRL)
772               Opc = SPU::ROTMr32;
773
774             hi32 = CurDAG->getMachineNode(Opc, dl, OpVT, SDValue(hi32, 0),
775                                           shift);
776           }
777
778           return hi32;
779         }
780       }
781     }
782   } else if (Opc == ISD::SHL) {
783     if (OpVT == MVT::i64)
784       return SelectSHLi64(N, OpVT);
785   } else if (Opc == ISD::SRL) {
786     if (OpVT == MVT::i64)
787       return SelectSRLi64(N, OpVT);
788   } else if (Opc == ISD::SRA) {
789     if (OpVT == MVT::i64)
790       return SelectSRAi64(N, OpVT);
791   } else if (Opc == ISD::FNEG
792              && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
793     DebugLoc dl = N->getDebugLoc();
794     // Check if the pattern is a special form of DFNMS:
795     // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
796     SDValue Op0 = N->getOperand(0);
797     if (Op0.getOpcode() == ISD::FSUB) {
798       SDValue Op00 = Op0.getOperand(0);
799       if (Op00.getOpcode() == ISD::FMUL) {
800         unsigned Opc = SPU::DFNMSf64;
801         if (OpVT == MVT::v2f64)
802           Opc = SPU::DFNMSv2f64;
803
804         return CurDAG->getMachineNode(Opc, dl, OpVT,
805                                       Op00.getOperand(0),
806                                       Op00.getOperand(1),
807                                       Op0.getOperand(1));
808       }
809     }
810
811     SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
812     SDNode *signMask = 0;
813     unsigned Opc = SPU::XORfneg64;
814
815     if (OpVT == MVT::f64) {
816       signMask = SelectI64Constant(negConst.getNode(), MVT::i64, dl);
817     } else if (OpVT == MVT::v2f64) {
818       Opc = SPU::XORfnegvec;
819       signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
820                                                  MVT::v2i64,
821                                                  negConst, negConst).getNode());
822     }
823
824     return CurDAG->getMachineNode(Opc, dl, OpVT,
825                                   N->getOperand(0), SDValue(signMask, 0));
826   } else if (Opc == ISD::FABS) {
827     if (OpVT == MVT::f64) {
828       SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
829       return CurDAG->getMachineNode(SPU::ANDfabs64, dl, OpVT,
830                                     N->getOperand(0), SDValue(signMask, 0));
831     } else if (OpVT == MVT::v2f64) {
832       SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
833       SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
834                                        absConst, absConst);
835       SDNode *signMask = emitBuildVector(absVec.getNode());
836       return CurDAG->getMachineNode(SPU::ANDfabsvec, dl, OpVT,
837                                     N->getOperand(0), SDValue(signMask, 0));
838     }
839   } else if (Opc == SPUISD::LDRESULT) {
840     // Custom select instructions for LDRESULT
841     EVT VT = N->getValueType(0);
842     SDValue Arg = N->getOperand(0);
843     SDValue Chain = N->getOperand(1);
844     SDNode *Result;
845
846     Result = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VT,
847                                     MVT::Other, Arg,
848                                     getRC( VT.getSimpleVT()), Chain);
849     return Result;
850
851   } else if (Opc == SPUISD::IndirectAddr) {
852     // Look at the operands: SelectCode() will catch the cases that aren't
853     // specifically handled here.
854     //
855     // SPUInstrInfo catches the following patterns:
856     // (SPUindirect (SPUhi ...), (SPUlo ...))
857     // (SPUindirect $sp, imm)
858     EVT VT = N->getValueType(0);
859     SDValue Op0 = N->getOperand(0);
860     SDValue Op1 = N->getOperand(1);
861     RegisterSDNode *RN;
862
863     if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
864         || (Op0.getOpcode() == ISD::Register
865             && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
866                 && RN->getReg() != SPU::R1))) {
867       NewOpc = SPU::Ar32;
868       Ops[1] = Op1;
869       if (Op1.getOpcode() == ISD::Constant) {
870         ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
871         Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
872         if (isInt<10>(CN->getSExtValue())) {
873           NewOpc = SPU::AIr32;
874           Ops[1] = Op1;
875         } else {
876           Ops[1] = SDValue(CurDAG->getMachineNode(SPU::ILr32, dl,
877                                                   N->getValueType(0),
878                                                   Op1),
879                            0);
880         }
881       }
882       Ops[0] = Op0;
883       n_ops = 2;
884     }
885   }
886
887   if (n_ops > 0) {
888     if (N->hasOneUse())
889       return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
890     else
891       return CurDAG->getMachineNode(NewOpc, dl, OpVT, Ops, n_ops);
892   } else
893     return SelectCode(N);
894 }
895
896 /*!
897  * Emit the instruction sequence for i64 left shifts. The basic algorithm
898  * is to fill the bottom two word slots with zeros so that zeros are shifted
899  * in as the entire quadword is shifted left.
900  *
901  * \note This code could also be used to implement v2i64 shl.
902  *
903  * @param Op The shl operand
904  * @param OpVT Op's machine value value type (doesn't need to be passed, but
905  * makes life easier.)
906  * @return The SDNode with the entire instruction sequence
907  */
908 SDNode *
909 SPUDAGToDAGISel::SelectSHLi64(SDNode *N, EVT OpVT) {
910   SDValue Op0 = N->getOperand(0);
911   EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
912                                OpVT, (128 / OpVT.getSizeInBits()));
913   SDValue ShiftAmt = N->getOperand(1);
914   EVT ShiftAmtVT = ShiftAmt.getValueType();
915   SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
916   SDValue SelMaskVal;
917   DebugLoc dl = N->getDebugLoc();
918
919   VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
920                                   Op0, getRC(MVT::v2i64) );
921   SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
922   SelMask = CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
923   ZeroFill = CurDAG->getMachineNode(SPU::ILv2i64, dl, VecVT,
924                                     CurDAG->getTargetConstant(0, OpVT));
925   VecOp0 = CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
926                                   SDValue(ZeroFill, 0),
927                                   SDValue(VecOp0, 0),
928                                   SDValue(SelMask, 0));
929
930   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
931     unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
932     unsigned bits = unsigned(CN->getZExtValue()) & 7;
933
934     if (bytes > 0) {
935       Shift =
936         CurDAG->getMachineNode(SPU::SHLQBYIv2i64, dl, VecVT,
937                                SDValue(VecOp0, 0),
938                                CurDAG->getTargetConstant(bytes, ShiftAmtVT));
939     }
940
941     if (bits > 0) {
942       Shift =
943         CurDAG->getMachineNode(SPU::SHLQBIIv2i64, dl, VecVT,
944                                SDValue((Shift != 0 ? Shift : VecOp0), 0),
945                                CurDAG->getTargetConstant(bits, ShiftAmtVT));
946     }
947   } else {
948     SDNode *Bytes =
949       CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
950                              ShiftAmt,
951                              CurDAG->getTargetConstant(3, ShiftAmtVT));
952     SDNode *Bits =
953       CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
954                              ShiftAmt,
955                              CurDAG->getTargetConstant(7, ShiftAmtVT));
956     Shift =
957       CurDAG->getMachineNode(SPU::SHLQBYv2i64, dl, VecVT,
958                              SDValue(VecOp0, 0), SDValue(Bytes, 0));
959     Shift =
960       CurDAG->getMachineNode(SPU::SHLQBIv2i64, dl, VecVT,
961                              SDValue(Shift, 0), SDValue(Bits, 0));
962   }
963
964   return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
965                                 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
966 }
967
968 /*!
969  * Emit the instruction sequence for i64 logical right shifts.
970  *
971  * @param Op The shl operand
972  * @param OpVT Op's machine value value type (doesn't need to be passed, but
973  * makes life easier.)
974  * @return The SDNode with the entire instruction sequence
975  */
976 SDNode *
977 SPUDAGToDAGISel::SelectSRLi64(SDNode *N, EVT OpVT) {
978   SDValue Op0 = N->getOperand(0);
979   EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
980                                OpVT, (128 / OpVT.getSizeInBits()));
981   SDValue ShiftAmt = N->getOperand(1);
982   EVT ShiftAmtVT = ShiftAmt.getValueType();
983   SDNode *VecOp0, *Shift = 0;
984   DebugLoc dl = N->getDebugLoc();
985
986   VecOp0 = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, VecVT,
987                                   Op0, getRC(MVT::v2i64) );
988
989   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
990     unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
991     unsigned bits = unsigned(CN->getZExtValue()) & 7;
992
993     if (bytes > 0) {
994       Shift =
995         CurDAG->getMachineNode(SPU::ROTQMBYIv2i64, dl, VecVT,
996                                SDValue(VecOp0, 0),
997                                CurDAG->getTargetConstant(bytes, ShiftAmtVT));
998     }
999
1000     if (bits > 0) {
1001       Shift =
1002         CurDAG->getMachineNode(SPU::ROTQMBIIv2i64, dl, VecVT,
1003                                SDValue((Shift != 0 ? Shift : VecOp0), 0),
1004                                CurDAG->getTargetConstant(bits, ShiftAmtVT));
1005     }
1006   } else {
1007     SDNode *Bytes =
1008       CurDAG->getMachineNode(SPU::ROTMIr32, dl, ShiftAmtVT,
1009                              ShiftAmt,
1010                              CurDAG->getTargetConstant(3, ShiftAmtVT));
1011     SDNode *Bits =
1012       CurDAG->getMachineNode(SPU::ANDIr32, dl, ShiftAmtVT,
1013                              ShiftAmt,
1014                              CurDAG->getTargetConstant(7, ShiftAmtVT));
1015
1016     // Ensure that the shift amounts are negated!
1017     Bytes = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1018                                    SDValue(Bytes, 0),
1019                                    CurDAG->getTargetConstant(0, ShiftAmtVT));
1020
1021     Bits = CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1022                                   SDValue(Bits, 0),
1023                                   CurDAG->getTargetConstant(0, ShiftAmtVT));
1024
1025     Shift =
1026       CurDAG->getMachineNode(SPU::ROTQMBYv2i64, dl, VecVT,
1027                              SDValue(VecOp0, 0), SDValue(Bytes, 0));
1028     Shift =
1029       CurDAG->getMachineNode(SPU::ROTQMBIv2i64, dl, VecVT,
1030                              SDValue(Shift, 0), SDValue(Bits, 0));
1031   }
1032
1033   return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1034                                 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
1035 }
1036
1037 /*!
1038  * Emit the instruction sequence for i64 arithmetic 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  */
1045 SDNode *
1046 SPUDAGToDAGISel::SelectSRAi64(SDNode *N, EVT OpVT) {
1047   // Promote Op0 to vector
1048   EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
1049                                OpVT, (128 / OpVT.getSizeInBits()));
1050   SDValue ShiftAmt = N->getOperand(1);
1051   EVT ShiftAmtVT = ShiftAmt.getValueType();
1052   DebugLoc dl = N->getDebugLoc();
1053
1054   SDNode *VecOp0 =
1055     CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1056                            VecVT, N->getOperand(0), getRC(MVT::v2i64));
1057
1058   SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
1059   SDNode *SignRot =
1060     CurDAG->getMachineNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
1061                            SDValue(VecOp0, 0), SignRotAmt);
1062   SDNode *UpperHalfSign =
1063     CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1064                            MVT::i32, SDValue(SignRot, 0), getRC(MVT::i32));
1065
1066   SDNode *UpperHalfSignMask =
1067     CurDAG->getMachineNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
1068   SDNode *UpperLowerMask =
1069     CurDAG->getMachineNode(SPU::FSMBIv2i64, dl, VecVT,
1070                            CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
1071   SDNode *UpperLowerSelect =
1072     CurDAG->getMachineNode(SPU::SELBv2i64, dl, VecVT,
1073                            SDValue(UpperHalfSignMask, 0),
1074                            SDValue(VecOp0, 0),
1075                            SDValue(UpperLowerMask, 0));
1076
1077   SDNode *Shift = 0;
1078
1079   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
1080     unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
1081     unsigned bits = unsigned(CN->getZExtValue()) & 7;
1082
1083     if (bytes > 0) {
1084       bytes = 31 - bytes;
1085       Shift =
1086         CurDAG->getMachineNode(SPU::ROTQBYIv2i64, dl, VecVT,
1087                                SDValue(UpperLowerSelect, 0),
1088                                CurDAG->getTargetConstant(bytes, ShiftAmtVT));
1089     }
1090
1091     if (bits > 0) {
1092       bits = 8 - bits;
1093       Shift =
1094         CurDAG->getMachineNode(SPU::ROTQBIIv2i64, dl, VecVT,
1095                                SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
1096                                CurDAG->getTargetConstant(bits, ShiftAmtVT));
1097     }
1098   } else {
1099     SDNode *NegShift =
1100       CurDAG->getMachineNode(SPU::SFIr32, dl, ShiftAmtVT,
1101                              ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
1102
1103     Shift =
1104       CurDAG->getMachineNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
1105                              SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
1106     Shift =
1107       CurDAG->getMachineNode(SPU::ROTQBIv2i64, dl, VecVT,
1108                              SDValue(Shift, 0), SDValue(NegShift, 0));
1109   }
1110
1111   return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1112                                 OpVT, SDValue(Shift, 0), getRC(MVT::i64));
1113 }
1114
1115 /*!
1116  Do the necessary magic necessary to load a i64 constant
1117  */
1118 SDNode *SPUDAGToDAGISel::SelectI64Constant(SDNode *N, EVT OpVT,
1119                                            DebugLoc dl) {
1120   ConstantSDNode *CN = cast<ConstantSDNode>(N);
1121   return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
1122 }
1123
1124 SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
1125                                            DebugLoc dl) {
1126   EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
1127   SDValue i64vec =
1128           SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
1129
1130   // Here's where it gets interesting, because we have to parse out the
1131   // subtree handed back in i64vec:
1132
1133   if (i64vec.getOpcode() == ISD::BITCAST) {
1134     // The degenerate case where the upper and lower bits in the splat are
1135     // identical:
1136     SDValue Op0 = i64vec.getOperand(0);
1137
1138     ReplaceUses(i64vec, Op0);
1139     return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1140                                   SDValue(emitBuildVector(Op0.getNode()), 0),
1141                                   getRC(MVT::i64));
1142   } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
1143     SDValue lhs = i64vec.getOperand(0);
1144     SDValue rhs = i64vec.getOperand(1);
1145     SDValue shufmask = i64vec.getOperand(2);
1146
1147     if (lhs.getOpcode() == ISD::BITCAST) {
1148       ReplaceUses(lhs, lhs.getOperand(0));
1149       lhs = lhs.getOperand(0);
1150     }
1151
1152     SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
1153                        ? lhs.getNode()
1154                        : emitBuildVector(lhs.getNode()));
1155
1156     if (rhs.getOpcode() == ISD::BITCAST) {
1157       ReplaceUses(rhs, rhs.getOperand(0));
1158       rhs = rhs.getOperand(0);
1159     }
1160
1161     SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
1162                        ? rhs.getNode()
1163                        : emitBuildVector(rhs.getNode()));
1164
1165     if (shufmask.getOpcode() == ISD::BITCAST) {
1166       ReplaceUses(shufmask, shufmask.getOperand(0));
1167       shufmask = shufmask.getOperand(0);
1168     }
1169
1170     SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
1171                             ? shufmask.getNode()
1172                             : emitBuildVector(shufmask.getNode()));
1173
1174    SDValue shufNode =
1175             CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
1176                                    SDValue(lhsNode, 0), SDValue(rhsNode, 0),
1177                                    SDValue(shufMaskNode, 0));
1178     HandleSDNode Dummy(shufNode);
1179     SDNode *SN = SelectCode(Dummy.getValue().getNode());
1180     if (SN == 0) SN = Dummy.getValue().getNode();
1181
1182     return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl,
1183                                   OpVT, SDValue(SN, 0), getRC(MVT::i64));
1184   } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
1185     return CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, dl, OpVT,
1186                                   SDValue(emitBuildVector(i64vec.getNode()), 0),
1187                                   getRC(MVT::i64));
1188   } else {
1189     report_fatal_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
1190                       "condition");
1191   }
1192 }
1193
1194 /// createSPUISelDag - This pass converts a legalized DAG into a
1195 /// SPU-specific DAG, ready for instruction scheduling.
1196 ///
1197 FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
1198   return new SPUDAGToDAGISel(TM);
1199 }