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