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