More CellSPU refinement and progress:
[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 "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/ADT/Statistic.h"
27 #include "llvm/Constants.h"
28 #include "llvm/GlobalValue.h"
29 #include "llvm/Intrinsics.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/Compiler.h"
33 #include <iostream>
34 #include <queue>
35 #include <set>
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->getValue());
45   }
46
47   //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
48   bool
49   isI32IntS10Immediate(ConstantSDNode *CN)
50   {
51     return isS10Constant((int) CN->getValue());
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((int) CN->getValue());
69   }
70
71   //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
72   bool
73   isI16IntS10Immediate(ConstantSDNode *CN)
74   {
75     return isS10Constant((short) CN->getValue());
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->getValue());
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::ValueType vt = CN->getValueType(0);
114     Imm = (short) CN->getValue();
115     if (vt >= MVT::i1 && vt <= MVT::i16) {
116       return true;
117     } else if (vt == MVT::i32) {
118       int32_t i_val = (int32_t) CN->getValue();
119       short s_val = (short) i_val;
120       return i_val == s_val;
121     } else {
122       int64_t i_val = (int64_t) CN->getValue();
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::ValueType 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   //===------------------------------------------------------------------===//
154   //! MVT::ValueType to "useful stuff" mapping structure:
155
156   struct valtype_map_s {
157     MVT::ValueType VT;
158     unsigned ldresult_ins;      /// LDRESULT instruction (0 = undefined)
159     int prefslot_byte;          /// Byte offset of the "preferred" slot
160     unsigned brcc_eq_ins;       /// br_cc equal instruction
161     unsigned brcc_neq_ins;      /// br_cc not equal instruction
162   };
163
164   const valtype_map_s valtype_map[] = {
165     { MVT::i1,  0,            3, 0,         0 },
166     { MVT::i8,  0,            3, 0,         0 },
167     { MVT::i16, SPU::ORHIr16, 2, SPU::BRHZ, SPU::BRHNZ },
168     { MVT::i32, SPU::ORIr32,  0, SPU::BRZ,  SPU::BRNZ },
169     { MVT::i64, SPU::ORIr64,  0, 0,         0 },
170     { MVT::f32, 0,            0, 0,         0 },
171     { MVT::f64, 0,            0, 0,         0 }
172   };
173
174   const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
175
176   const valtype_map_s *getValueTypeMapEntry(MVT::ValueType VT)
177   {
178     const valtype_map_s *retval = 0;
179     for (size_t i = 0; i < n_valtype_map; ++i) {
180       if (valtype_map[i].VT == VT) {
181         retval = valtype_map + i;
182         break;
183       }
184     }
185
186
187 #ifndef NDEBUG
188     if (retval == 0) {
189       cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
190            << MVT::getValueTypeString(VT)
191            << "\n";
192       abort();
193     }
194 #endif
195
196     return retval;
197   }
198 }
199
200 //===--------------------------------------------------------------------===//
201 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
202 /// instructions for SelectionDAG operations.
203 ///
204 class SPUDAGToDAGISel :
205   public SelectionDAGISel
206 {
207   SPUTargetMachine &TM;
208   SPUTargetLowering &SPUtli;
209   unsigned GlobalBaseReg;
210
211 public:
212   SPUDAGToDAGISel(SPUTargetMachine &tm) :
213     SelectionDAGISel(*tm.getTargetLowering()),
214     TM(tm),
215     SPUtli(*tm.getTargetLowering())
216   {}
217     
218   virtual bool runOnFunction(Function &Fn) {
219     // Make sure we re-emit a set of the global base reg if necessary
220     GlobalBaseReg = 0;
221     SelectionDAGISel::runOnFunction(Fn);
222     return true;
223   }
224    
225   /// getI32Imm - Return a target constant with the specified value, of type
226   /// i32.
227   inline SDOperand getI32Imm(uint32_t Imm) {
228     return CurDAG->getTargetConstant(Imm, MVT::i32);
229   }
230
231   /// getI64Imm - Return a target constant with the specified value, of type
232   /// i64.
233   inline SDOperand getI64Imm(uint64_t Imm) {
234     return CurDAG->getTargetConstant(Imm, MVT::i64);
235   }
236     
237   /// getSmallIPtrImm - Return a target constant of pointer type.
238   inline SDOperand getSmallIPtrImm(unsigned Imm) {
239     return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
240   }
241
242   /// Select - Convert the specified operand from a target-independent to a
243   /// target-specific node if it hasn't already been changed.
244   SDNode *Select(SDOperand Op);
245
246   /// Return true if the address N is a RI7 format address [r+imm]
247   bool SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
248                         SDOperand &Base);
249
250   //! Returns true if the address N is an A-form (local store) address
251   bool SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
252                        SDOperand &Index);
253
254   //! D-form address predicate
255   bool SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
256                        SDOperand &Index);
257
258   //! Address predicate if N can be expressed as an indexed [r+r] operation.
259   bool SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
260                        SDOperand &Index);
261
262   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
263   /// inline asm expressions.
264   virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
265                                             char ConstraintCode,
266                                             std::vector<SDOperand> &OutOps,
267                                             SelectionDAG &DAG) {
268     SDOperand Op0, Op1;
269     switch (ConstraintCode) {
270     default: return true;
271     case 'm':   // memory
272       if (!SelectDFormAddr(Op, Op, Op0, Op1) 
273           && !SelectAFormAddr(Op, Op, Op0, Op1))
274         SelectXFormAddr(Op, Op, Op0, Op1);
275       break;
276     case 'o':   // offsetable
277       if (!SelectDFormAddr(Op, Op, Op0, Op1)
278           && !SelectAFormAddr(Op, Op, Op0, Op1)) {
279         Op0 = Op;
280         AddToISelQueue(Op0);     // r+0.
281         Op1 = getSmallIPtrImm(0);
282       }
283       break;
284     case 'v':   // not offsetable
285 #if 1
286       assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
287 #else
288       SelectAddrIdxOnly(Op, Op, Op0, Op1);
289 #endif
290       break;
291     }
292       
293     OutOps.push_back(Op0);
294     OutOps.push_back(Op1);
295     return false;
296   }
297
298   /// InstructionSelectBasicBlock - This callback is invoked by
299   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
300   virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
301
302   virtual const char *getPassName() const {
303     return "Cell SPU DAG->DAG Pattern Instruction Selection";
304   } 
305     
306   /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
307   /// this target when scheduling the DAG.
308   virtual HazardRecognizer *CreateTargetHazardRecognizer() {
309     const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo();
310     assert(II && "No InstrInfo?");
311     return new SPUHazardRecognizer(*II); 
312   }
313
314   // Include the pieces autogenerated from the target description.
315 #include "SPUGenDAGISel.inc"
316 };
317
318 /// InstructionSelectBasicBlock - This callback is invoked by
319 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
320 void
321 SPUDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG)
322 {
323   DEBUG(BB->dump());
324
325   // Select target instructions for the DAG.
326   DAG.setRoot(SelectRoot(DAG.getRoot()));
327   DAG.RemoveDeadNodes();
328   
329   // Emit machine code to BB.
330   ScheduleAndEmitDAG(DAG);
331 }
332
333 bool 
334 SPUDAGToDAGISel::SelectDForm2Addr(SDOperand Op, SDOperand N, SDOperand &Disp,
335                                   SDOperand &Base) {
336   unsigned Opc = N.getOpcode();
337   unsigned VT = N.getValueType();
338   MVT::ValueType PtrVT = SPUtli.getPointerTy();
339   ConstantSDNode *CN = 0;
340   int Imm;
341
342   if (Opc == ISD::ADD) {
343     SDOperand Op0 = N.getOperand(0);
344     SDOperand Op1 = N.getOperand(1);
345     if (Op1.getOpcode() == ISD::Constant ||
346         Op1.getOpcode() == ISD::TargetConstant) {
347       CN = cast<ConstantSDNode>(Op1);
348       Imm = int(CN->getValue());
349       if (Imm <= 0xff) {
350         Disp = CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
351         Base = Op0;
352         return true;
353       }
354     }
355   } else if (Opc == ISD::GlobalAddress
356              || Opc == ISD::TargetGlobalAddress
357              || Opc == ISD::Register) {
358     // Plain old local store address: 
359     Disp = CurDAG->getTargetConstant(0, VT);
360     Base = N;
361     return true;
362   } else if (Opc == SPUISD::DFormAddr) {
363     // D-Form address: This is pretty straightforward, naturally...
364     CN = cast<ConstantSDNode>(N.getOperand(1));
365     assert(CN != 0 && "SelectDFormAddr/SPUISD::DForm2Addr expecting constant");
366     Imm = unsigned(CN->getValue());
367     if (Imm < 0xff) {
368       Disp = CurDAG->getTargetConstant(CN->getValue(), PtrVT);
369       Base = N.getOperand(0);
370       return true;
371     }
372   }
373
374   return false;
375 }
376
377 /*!
378  \arg Op The ISD instructio operand
379  \arg N The address to be tested
380  \arg Base The base address
381  \arg Index The base address index
382  */
383 bool
384 SPUDAGToDAGISel::SelectAFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
385                     SDOperand &Index) {
386   // These match the addr256k operand type:
387   MVT::ValueType OffsVT = MVT::i16;
388   MVT::ValueType PtrVT = SPUtli.getPointerTy();
389
390   switch (N.getOpcode()) {
391   case ISD::Constant:
392   case ISD::ConstantPool:
393   case ISD::GlobalAddress:
394     cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
395     abort();
396     /*NOTREACHED*/
397
398   case ISD::TargetConstant: {
399     // Loading from a constant address.
400     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
401     int Imm = (int)CN->getValue();
402     if (Imm < 0x3ffff && (Imm & 0x3) == 0) {
403       Base = CurDAG->getTargetConstant(Imm, PtrVT);
404       // Note that this operand will be ignored by the assembly printer...
405       Index = CurDAG->getTargetConstant(0, OffsVT);
406       return true;
407     }
408   }
409   case ISD::TargetGlobalAddress:
410   case ISD::TargetConstantPool:
411   case SPUISD::AFormAddr: {
412     // The address is in Base. N is a dummy that will be ignored by
413     // the assembly printer.
414     Base = N;
415     Index = CurDAG->getTargetConstant(0, OffsVT);
416     return true;
417   }
418   }
419
420   return false;
421 }
422
423 /*!
424   \arg Op The ISD instruction (ignored)
425   \arg N The address to be tested
426   \arg Base Base address register/pointer
427   \arg Index Base address index
428
429   Examine the input address by a base register plus a signed 10-bit
430   displacement, [r+I10] (D-form address).
431
432   \return true if \a N is a D-form address with \a Base and \a Index set
433   to non-empty SDOperand instances.
434 */
435 bool
436 SPUDAGToDAGISel::SelectDFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
437                                  SDOperand &Index) {
438   unsigned Opc = N.getOpcode();
439   unsigned PtrTy = SPUtli.getPointerTy();
440
441   if (Opc == ISD::Register) {
442     Base = N;
443     Index = CurDAG->getTargetConstant(0, PtrTy);
444     return true;
445   } else if (Opc == ISD::FrameIndex) {
446     FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N);
447     DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
448                << FI->getIndex() << "\n");
449     if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) {
450       Base = CurDAG->getTargetConstant(0, PtrTy);
451       Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
452       return true;
453     }
454   } else if (Opc == ISD::ADD) {
455     // Generated by getelementptr
456     const SDOperand Op0 = N.getOperand(0); // Frame index/base
457     const SDOperand Op1 = N.getOperand(1); // Offset within base
458
459     if (Op1.getOpcode() == ISD::Constant
460         || Op1.getOpcode() == ISD::TargetConstant) {
461       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
462       assert(CN != 0 && "SelectDFormAddr: Expected a constant");
463
464       int32_t offset = (int32_t) CN->getSignExtended();
465       unsigned Opc0 = Op0.getOpcode();
466
467       if ((offset & 0xf) != 0) {
468         // Unaligned offset: punt and let X-form address handle it.
469         // NOTE: This really doesn't have to be strictly 16-byte aligned,
470         // since the load/store quadword instructions will implicitly
471         // zero the lower 4 bits of the resulting address.
472         return false;
473       }
474
475       if (Opc0 == ISD::FrameIndex) {
476         FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Op0);
477         DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
478               << " frame index = " << FI->getIndex() << "\n");
479
480         if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) {
481           Base = CurDAG->getTargetConstant(offset, PtrTy);
482           Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
483           return true;
484         }
485       } else if (offset > SPUFrameInfo::minFrameOffset()
486                  && offset < SPUFrameInfo::maxFrameOffset()) {
487         Base = CurDAG->getTargetConstant(offset, PtrTy);
488         if (Opc0 == ISD::GlobalAddress) {
489           // Convert global address to target global address
490           GlobalAddressSDNode *GV = dyn_cast<GlobalAddressSDNode>(Op0);
491           Index = CurDAG->getTargetGlobalAddress(GV->getGlobal(), PtrTy);
492           return true;
493         } else {
494           // Otherwise, just take operand 0
495           Index = Op0;
496           return true;
497         }
498       }
499     } else
500       return false;
501   } else if (Opc == SPUISD::DFormAddr) {
502     // D-Form address: This is pretty straightforward, naturally...
503     ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1));
504     assert(CN != 0 && "SelectDFormAddr/SPUISD::DFormAddr expecting constant"); 
505     Base = CurDAG->getTargetConstant(CN->getValue(), PtrTy);
506     Index = N.getOperand(0);
507     return true;
508   } else if (Opc == ISD::FrameIndex) {
509     // Stack frame index must be less than 512 (divided by 16):
510     FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N);
511     DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
512           << FI->getIndex() << "\n");
513     if (FI->getIndex() < SPUFrameInfo::maxFrameOffset()) {
514       Base = CurDAG->getTargetConstant(0, PtrTy);
515       Index = CurDAG->getTargetFrameIndex(FI->getIndex(), PtrTy);
516       return true;
517     }
518   }
519
520   return false;
521 }
522
523 /*!
524   \arg Op The ISD instruction operand
525   \arg N The address operand
526   \arg Base The base pointer operand
527   \arg Index The offset/index operand
528
529   If the address \a N can be expressed as a [r + s10imm] address, returns false.
530   Otherwise, creates two operands, Base and Index that will become the [r+r]
531   address.
532 */
533 bool
534 SPUDAGToDAGISel::SelectXFormAddr(SDOperand Op, SDOperand N, SDOperand &Base,
535                                  SDOperand &Index) {
536   if (SelectAFormAddr(Op, N, Base, Index)
537       || SelectDFormAddr(Op, N, Base, Index))
538     return false;
539
540   unsigned Opc = N.getOpcode();
541
542   if (Opc == ISD::ADD) {
543     SDOperand N1 = N.getOperand(0);
544     SDOperand N2 = N.getOperand(1);
545     unsigned N1Opc = N1.getOpcode();
546     unsigned N2Opc = N2.getOpcode();
547
548     if ((N1Opc == SPUISD::Hi && N2Opc == SPUISD::Lo)
549          || (N1Opc == SPUISD::Lo && N2Opc == SPUISD::Hi)
550         || (N1Opc == SPUISD::XFormAddr)) {
551       Base = N.getOperand(0);
552       Index = N.getOperand(1);
553       return true;
554     } else {
555       cerr << "SelectXFormAddr: Unhandled ADD operands:\n";
556       N1.Val->dump();
557       cerr << "\n";
558       N2.Val->dump();
559       cerr << "\n";
560       abort();
561       /*UNREACHED*/
562     }
563   } else if (Opc == SPUISD::XFormAddr) {
564     Base = N;
565     Index = N.getOperand(1);
566     return true;
567   } else if (N.getNumOperands() == 2) {
568     SDOperand N1 = N.getOperand(0);
569     SDOperand N2 = N.getOperand(1);
570     unsigned N1Opc = N1.getOpcode();
571     unsigned N2Opc = N2.getOpcode();
572
573     if ((N1Opc == ISD::CopyToReg || N1Opc == ISD::Register)
574         && (N2Opc == ISD::CopyToReg || N2Opc == ISD::Register)) {
575       Base = N.getOperand(0);
576       Index = N.getOperand(1);
577       return true;
578       /*UNREACHED*/
579     } else {
580       cerr << "SelectXFormAddr: 2-operand unhandled operand:\n";
581       N.Val->dump();
582       cerr << "\n";
583       abort();
584     /*UNREACHED*/
585     }
586   } else {
587     cerr << "SelectXFormAddr: Unhandled operand type:\n";
588     N.Val->dump();
589     cerr << "\n";
590     abort();
591     /*UNREACHED*/
592   }
593
594   return false;
595 }
596
597 //! Convert the operand from a target-independent to a target-specific node
598 /*!
599  */
600 SDNode *
601 SPUDAGToDAGISel::Select(SDOperand Op) {
602   SDNode *N = Op.Val;
603   unsigned Opc = N->getOpcode();
604
605   if (Opc >= ISD::BUILTIN_OP_END && Opc < SPUISD::FIRST_NUMBER) {
606     return NULL;   // Already selected.
607   } else if (Opc == ISD::FrameIndex) {
608     // Selects to AIr32 FI, 0 which in turn will become AIr32 SP, imm.
609     int FI = cast<FrameIndexSDNode>(N)->getIndex();
610     MVT::ValueType PtrVT = SPUtli.getPointerTy();
611     SDOperand Zero = CurDAG->getTargetConstant(0, PtrVT);
612     SDOperand TFI = CurDAG->getTargetFrameIndex(FI, PtrVT);
613
614     DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AI32 <FI>, 0\n");
615     if (N->hasOneUse())
616       return CurDAG->SelectNodeTo(N, SPU::AIr32, Op.getValueType(), TFI, Zero);
617     CurDAG->getTargetNode(SPU::AIr32, Op.getValueType(), TFI, Zero);
618   } else if (Opc == SPUISD::LDRESULT) {
619     // Custom select instructions for LDRESULT
620     unsigned VT = N->getValueType(0);
621     SDOperand Arg = N->getOperand(0);
622     SDOperand Chain = N->getOperand(1);
623     SDNode *Result;
624
625     AddToISelQueue(Arg);
626     if (!MVT::isFloatingPoint(VT)) {
627       SDOperand Zero = CurDAG->getTargetConstant(0, VT);
628       const valtype_map_s *vtm = getValueTypeMapEntry(VT);
629
630       if (vtm->ldresult_ins == 0) {
631         cerr << "LDRESULT for unsupported type: "
632              << MVT::getValueTypeString(VT)
633              << "\n";
634         abort();
635       } else
636         Opc = vtm->ldresult_ins;
637
638       AddToISelQueue(Zero);
639       Result = CurDAG->SelectNodeTo(N, Opc, VT, MVT::Other, Arg, Zero, Chain);
640     } else {
641       Result =
642         CurDAG->SelectNodeTo(N, (VT == MVT::f32 ? SPU::ORf32 : SPU::ORf64),
643                              MVT::Other, Arg, Arg, Chain);
644     }
645
646     Chain = SDOperand(Result, 1);
647     AddToISelQueue(Chain);
648
649     return Result;
650   }
651   
652   return SelectCode(Op);
653 }
654
655 /// createPPCISelDag - This pass converts a legalized DAG into a 
656 /// SPU-specific DAG, ready for instruction scheduling.
657 ///
658 FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
659   return new SPUDAGToDAGISel(TM);
660 }