Rename SDOperand to SDValue.
[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 "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Constants.h"
29 #include "llvm/GlobalValue.h"
30 #include "llvm/Intrinsics.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/Compiler.h"
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->getSignExtended());
45   }
46
47   //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
48   bool
49   isI32IntS10Immediate(ConstantSDNode *CN)
50   {
51     return isS10Constant(CN->getSignExtended());
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->getSignExtended());
69   }
70
71   //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
72   bool
73   isI16IntS10Immediate(ConstantSDNode *CN)
74   {
75     return isS10Constant(CN->getSignExtended());
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 vt = CN->getValueType(0);
114     Imm = (short) CN->getValue();
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->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 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     int prefslot_byte;          /// Byte offset of the "preferred" slot
171   };
172
173   const valtype_map_s valtype_map[] = {
174     { MVT::i1,    0,            false, 3 },
175     { MVT::i8,    SPU::ORBIr8,  true,  3 },
176     { MVT::i16,   SPU::ORHIr16, true,  2 },
177     { MVT::i32,   SPU::ORIr32,  true,  0 },
178     { MVT::i64,   SPU::ORr64,   false, 0 },
179     { MVT::f32,   SPU::ORf32,   false, 0 },
180     { MVT::f64,   SPU::ORf64,   false, 0 },
181     // vector types... (sigh!)
182     { MVT::v16i8, 0,            false, 0 },
183     { MVT::v8i16, 0,            false, 0 },
184     { MVT::v4i32, 0,            false, 0 },
185     { MVT::v2i64, 0,            false, 0 },
186     { MVT::v4f32, 0,            false, 0 },
187     { MVT::v2f64, 0,            false, 0 }
188   };
189
190   const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
191
192   const valtype_map_s *getValueTypeMapEntry(MVT VT)
193   {
194     const valtype_map_s *retval = 0;
195     for (size_t i = 0; i < n_valtype_map; ++i) {
196       if (valtype_map[i].VT == VT) {
197         retval = valtype_map + i;
198         break;
199       }
200     }
201
202
203 #ifndef NDEBUG
204     if (retval == 0) {
205       cerr << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
206            << VT.getMVTString()
207            << "\n";
208       abort();
209     }
210 #endif
211
212     return retval;
213   }
214 }
215
216 namespace {
217
218 //===--------------------------------------------------------------------===//
219 /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
220 /// instructions for SelectionDAG operations.
221 ///
222 class SPUDAGToDAGISel :
223   public SelectionDAGISel
224 {
225   SPUTargetMachine &TM;
226   SPUTargetLowering &SPUtli;
227   unsigned GlobalBaseReg;
228
229 public:
230   explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
231     SelectionDAGISel(*tm.getTargetLowering()),
232     TM(tm),
233     SPUtli(*tm.getTargetLowering())
234   {}
235     
236   virtual bool runOnFunction(Function &Fn) {
237     // Make sure we re-emit a set of the global base reg if necessary
238     GlobalBaseReg = 0;
239     SelectionDAGISel::runOnFunction(Fn);
240     return true;
241   }
242    
243   /// getI32Imm - Return a target constant with the specified value, of type
244   /// i32.
245   inline SDValue getI32Imm(uint32_t Imm) {
246     return CurDAG->getTargetConstant(Imm, MVT::i32);
247   }
248
249   /// getI64Imm - Return a target constant with the specified value, of type
250   /// i64.
251   inline SDValue getI64Imm(uint64_t Imm) {
252     return CurDAG->getTargetConstant(Imm, MVT::i64);
253   }
254     
255   /// getSmallIPtrImm - Return a target constant of pointer type.
256   inline SDValue getSmallIPtrImm(unsigned Imm) {
257     return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
258   }
259
260   /// Select - Convert the specified operand from a target-independent to a
261   /// target-specific node if it hasn't already been changed.
262   SDNode *Select(SDValue Op);
263
264   //! Returns true if the address N is an A-form (local store) address
265   bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
266                        SDValue &Index);
267
268   //! D-form address predicate
269   bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
270                        SDValue &Index);
271
272   /// Alternate D-form address using i7 offset predicate
273   bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
274                         SDValue &Base);
275
276   /// D-form address selection workhorse
277   bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp,
278                              SDValue &Base, int minOffset, int maxOffset);
279
280   //! Address predicate if N can be expressed as an indexed [r+r] operation.
281   bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
282                        SDValue &Index);
283
284   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
285   /// inline asm expressions.
286   virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
287                                             char ConstraintCode,
288                                             std::vector<SDValue> &OutOps,
289                                             SelectionDAG &DAG) {
290     SDValue Op0, Op1;
291     switch (ConstraintCode) {
292     default: return true;
293     case 'm':   // memory
294       if (!SelectDFormAddr(Op, Op, Op0, Op1) 
295           && !SelectAFormAddr(Op, Op, Op0, Op1))
296         SelectXFormAddr(Op, Op, Op0, Op1);
297       break;
298     case 'o':   // offsetable
299       if (!SelectDFormAddr(Op, Op, Op0, Op1)
300           && !SelectAFormAddr(Op, Op, Op0, Op1)) {
301         Op0 = Op;
302         AddToISelQueue(Op0);     // r+0.
303         Op1 = getSmallIPtrImm(0);
304       }
305       break;
306     case 'v':   // not offsetable
307 #if 1
308       assert(0 && "InlineAsmMemoryOperand 'v' constraint not handled.");
309 #else
310       SelectAddrIdxOnly(Op, Op, Op0, Op1);
311 #endif
312       break;
313     }
314       
315     OutOps.push_back(Op0);
316     OutOps.push_back(Op1);
317     return false;
318   }
319
320   /// InstructionSelect - This callback is invoked by
321   /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
322   virtual void InstructionSelect(SelectionDAG &DAG);
323
324   virtual const char *getPassName() const {
325     return "Cell SPU DAG->DAG Pattern Instruction Selection";
326   } 
327     
328   /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
329   /// this target when scheduling the DAG.
330   virtual HazardRecognizer *CreateTargetHazardRecognizer() {
331     const TargetInstrInfo *II = SPUtli.getTargetMachine().getInstrInfo();
332     assert(II && "No InstrInfo?");
333     return new SPUHazardRecognizer(*II); 
334   }
335
336   // Include the pieces autogenerated from the target description.
337 #include "SPUGenDAGISel.inc"
338 };
339
340 }
341
342 /// InstructionSelect - This callback is invoked by
343 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
344 void
345 SPUDAGToDAGISel::InstructionSelect(SelectionDAG &DAG)
346 {
347   DEBUG(BB->dump());
348
349   // Select target instructions for the DAG.
350   DAG.setRoot(SelectRoot(DAG.getRoot()));
351   DAG.RemoveDeadNodes();
352 }
353
354 /*!
355  \arg Op The ISD instructio operand
356  \arg N The address to be tested
357  \arg Base The base address
358  \arg Index The base address index
359  */
360 bool
361 SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
362                     SDValue &Index) {
363   // These match the addr256k operand type:
364   MVT OffsVT = MVT::i16;
365   SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
366
367   switch (N.getOpcode()) {
368   case ISD::Constant:
369   case ISD::ConstantPool:
370   case ISD::GlobalAddress:
371     cerr << "SPU SelectAFormAddr: Constant/Pool/Global not lowered.\n";
372     abort();
373     /*NOTREACHED*/
374
375   case ISD::TargetConstant:
376   case ISD::TargetGlobalAddress:
377   case ISD::TargetJumpTable:
378     cerr << "SPUSelectAFormAddr: Target Constant/Pool/Global not wrapped as "
379          << "A-form address.\n";
380     abort();
381     /*NOTREACHED*/
382
383   case SPUISD::AFormAddr: 
384     // Just load from memory if there's only a single use of the location,
385     // otherwise, this will get handled below with D-form offset addresses
386     if (N.hasOneUse()) {
387       SDValue Op0 = N.getOperand(0);
388       switch (Op0.getOpcode()) {
389       case ISD::TargetConstantPool:
390       case ISD::TargetJumpTable:
391         Base = Op0;
392         Index = Zero;
393         return true;
394
395       case ISD::TargetGlobalAddress: {
396         GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
397         GlobalValue *GV = GSDN->getGlobal();
398         if (GV->getAlignment() == 16) {
399           Base = Op0;
400           Index = Zero;
401           return true;
402         }
403         break;
404       }
405       }
406     }
407     break;
408   }
409   return false;
410 }
411
412 bool 
413 SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
414                                   SDValue &Base) {
415   const int minDForm2Offset = -(1 << 7);
416   const int maxDForm2Offset = (1 << 7) - 1;
417   return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
418                                maxDForm2Offset);
419 }
420
421 /*!
422   \arg Op The ISD instruction (ignored)
423   \arg N The address to be tested
424   \arg Base Base address register/pointer
425   \arg Index Base address index
426
427   Examine the input address by a base register plus a signed 10-bit
428   displacement, [r+I10] (D-form address).
429
430   \return true if \a N is a D-form address with \a Base and \a Index set
431   to non-empty SDValue instances.
432 */
433 bool
434 SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
435                                  SDValue &Index) {
436   return DFormAddressPredicate(Op, N, Base, Index,
437                               SPUFrameInfo::minFrameOffset(),
438                               SPUFrameInfo::maxFrameOffset());
439 }
440
441 bool
442 SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base,
443                                       SDValue &Index, int minOffset,
444                                       int maxOffset) {
445   unsigned Opc = N.getOpcode();
446   MVT PtrTy = SPUtli.getPointerTy();
447
448   if (Opc == ISD::FrameIndex) {
449     // Stack frame index must be less than 512 (divided by 16):
450     FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
451     int FI = int(FIN->getIndex());
452     DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
453                << FI << "\n");
454     if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
455       Base = CurDAG->getTargetConstant(0, PtrTy);
456       Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
457       return true;
458     }
459   } else if (Opc == ISD::ADD) {
460     // Generated by getelementptr
461     const SDValue Op0 = N.getOperand(0);
462     const SDValue Op1 = N.getOperand(1);
463
464     if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
465         || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
466       Base = CurDAG->getTargetConstant(0, PtrTy);
467       Index = N;
468       return true;
469     } else if (Op1.getOpcode() == ISD::Constant
470                || Op1.getOpcode() == ISD::TargetConstant) {
471       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
472       int32_t offset = int32_t(CN->getSignExtended());
473
474       if (Op0.getOpcode() == ISD::FrameIndex) {
475         FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
476         int FI = int(FIN->getIndex());
477         DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
478                    << " frame index = " << FI << "\n");
479
480         if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
481           Base = CurDAG->getTargetConstant(offset, PtrTy);
482           Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
483           return true;
484         }
485       } else if (offset > minOffset && offset < maxOffset) {
486         Base = CurDAG->getTargetConstant(offset, PtrTy);
487         Index = Op0;
488         return true;
489       }
490     } else if (Op0.getOpcode() == ISD::Constant
491                || Op0.getOpcode() == ISD::TargetConstant) {
492       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
493       int32_t offset = int32_t(CN->getSignExtended());
494
495       if (Op1.getOpcode() == ISD::FrameIndex) {
496         FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
497         int FI = int(FIN->getIndex());
498         DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
499                    << " frame index = " << FI << "\n");
500
501         if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
502           Base = CurDAG->getTargetConstant(offset, PtrTy);
503           Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
504           return true;
505         }
506       } else if (offset > minOffset && offset < maxOffset) {
507         Base = CurDAG->getTargetConstant(offset, PtrTy);
508         Index = Op1;
509         return true;
510       }
511     }
512   } else if (Opc == SPUISD::IndirectAddr) {
513     // Indirect with constant offset -> D-Form address
514     const SDValue Op0 = N.getOperand(0);
515     const SDValue Op1 = N.getOperand(1);
516
517     if (Op0.getOpcode() == SPUISD::Hi
518         && Op1.getOpcode() == SPUISD::Lo) {
519       // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
520       Base = CurDAG->getTargetConstant(0, PtrTy);
521       Index = N;
522       return true;
523     } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
524       int32_t offset = 0;
525       SDValue idxOp;
526
527       if (isa<ConstantSDNode>(Op1)) {
528         ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
529         offset = int32_t(CN->getSignExtended());
530         idxOp = Op0;
531       } else if (isa<ConstantSDNode>(Op0)) {
532         ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
533         offset = int32_t(CN->getSignExtended());
534         idxOp = Op1;
535       } 
536
537       if (offset >= minOffset && offset <= maxOffset) {
538         Base = CurDAG->getTargetConstant(offset, PtrTy);
539         Index = idxOp;
540         return true;
541       }
542     }
543   } else if (Opc == SPUISD::AFormAddr) {
544     Base = CurDAG->getTargetConstant(0, N.getValueType());
545     Index = N;
546     return true;
547   } else if (Opc == SPUISD::LDRESULT) {
548     Base = CurDAG->getTargetConstant(0, N.getValueType());
549     Index = N;
550     return true;
551   }
552   return false;
553 }
554
555 /*!
556   \arg Op The ISD instruction operand
557   \arg N The address operand
558   \arg Base The base pointer operand
559   \arg Index The offset/index operand
560
561   If the address \a N can be expressed as a [r + s10imm] address, returns false.
562   Otherwise, creates two operands, Base and Index that will become the [r+r]
563   address.
564 */
565 bool
566 SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
567                                  SDValue &Index) {
568   if (SelectAFormAddr(Op, N, Base, Index)
569       || SelectDFormAddr(Op, N, Base, Index))
570     return false;
571
572   // All else fails, punt and use an X-form address:
573   Base = N.getOperand(0);
574   Index = N.getOperand(1);
575   return true;
576 }
577
578 //! Convert the operand from a target-independent to a target-specific node
579 /*!
580  */
581 SDNode *
582 SPUDAGToDAGISel::Select(SDValue Op) {
583   SDNode *N = Op.Val;
584   unsigned Opc = N->getOpcode();
585   int n_ops = -1;
586   unsigned NewOpc;
587   MVT OpVT = Op.getValueType();
588   SDValue Ops[8];
589
590   if (N->isMachineOpcode()) {
591     return NULL;   // Already selected.
592   } else if (Opc == ISD::FrameIndex) {
593     // Selects to (add $sp, FI * stackSlotSize)
594     int FI =
595       SPUFrameInfo::FItoStackOffset(cast<FrameIndexSDNode>(N)->getIndex());
596     MVT PtrVT = SPUtli.getPointerTy();
597
598     // Adjust stack slot to actual offset in frame:
599     if (isS10Constant(FI)) {
600       DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with AIr32 $sp, "
601                  << FI
602                  << "\n");
603       NewOpc = SPU::AIr32;
604       Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
605       Ops[1] = CurDAG->getTargetConstant(FI, PtrVT);
606       n_ops = 2;
607     } else {
608       DEBUG(cerr << "SPUDAGToDAGISel: Replacing FrameIndex with Ar32 $sp, "
609                  << FI
610                  << "\n");
611       NewOpc = SPU::Ar32;
612       Ops[0] = CurDAG->getRegister(SPU::R1, PtrVT);
613       Ops[1] = CurDAG->getConstant(FI, PtrVT);
614       n_ops = 2;
615
616       AddToISelQueue(Ops[1]);
617     }
618   } else if (Opc == ISD::ZERO_EXTEND) {
619     // (zero_extend:i16 (and:i8 <arg>, <const>))
620     const SDValue &Op1 = N->getOperand(0);
621
622     if (Op.getValueType() == MVT::i16 && Op1.getValueType() == MVT::i8) {
623       if (Op1.getOpcode() == ISD::AND) {
624         // Fold this into a single ANDHI. This is often seen in expansions of i1
625         // to i8, then i8 to i16 in logical/branching operations.
626         DEBUG(cerr << "CellSPU: Coalescing (zero_extend:i16 (and:i8 "
627                       "<arg>, <const>))\n");
628         NewOpc = SPU::ANDHIi8i16;
629         Ops[0] = Op1.getOperand(0);
630         Ops[1] = Op1.getOperand(1);
631         n_ops = 2;
632       }
633     }
634   } else if (Opc == SPUISD::LDRESULT) {
635     // Custom select instructions for LDRESULT
636     MVT VT = N->getValueType(0);
637     SDValue Arg = N->getOperand(0);
638     SDValue Chain = N->getOperand(1);
639     SDNode *Result;
640     const valtype_map_s *vtm = getValueTypeMapEntry(VT);
641
642     if (vtm->ldresult_ins == 0) {
643       cerr << "LDRESULT for unsupported type: "
644            << VT.getMVTString()
645            << "\n";
646       abort();
647     }
648
649     AddToISelQueue(Arg);
650     Opc = vtm->ldresult_ins;
651     if (vtm->ldresult_imm) {
652       SDValue Zero = CurDAG->getTargetConstant(0, VT);
653
654       AddToISelQueue(Zero);
655       Result = CurDAG->getTargetNode(Opc, VT, MVT::Other, Arg, Zero, Chain);
656     } else {
657       Result = CurDAG->getTargetNode(Opc, MVT::Other, Arg, Arg, Chain);
658     }
659
660     Chain = SDValue(Result, 1);
661     AddToISelQueue(Chain);
662
663     return Result;
664   } else if (Opc == SPUISD::IndirectAddr) {
665     SDValue Op0 = Op.getOperand(0);
666     if (Op0.getOpcode() == SPUISD::LDRESULT) {
667         /* || Op0.getOpcode() == SPUISD::AFormAddr) */
668       // (IndirectAddr (LDRESULT, imm))
669       SDValue Op1 = Op.getOperand(1);
670       MVT VT = Op.getValueType();
671
672       DEBUG(cerr << "CellSPU: IndirectAddr(LDRESULT, imm):\nOp0 = ");
673       DEBUG(Op.getOperand(0).Val->dump(CurDAG));
674       DEBUG(cerr << "\nOp1 = ");
675       DEBUG(Op.getOperand(1).Val->dump(CurDAG));
676       DEBUG(cerr << "\n");
677
678       if (Op1.getOpcode() == ISD::Constant) {
679         ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
680         Op1 = CurDAG->getTargetConstant(CN->getValue(), VT);
681         NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
682         AddToISelQueue(Op0);
683         AddToISelQueue(Op1);
684         Ops[0] = Op0;
685         Ops[1] = Op1;
686         n_ops = 2;
687       }
688     }
689   }
690   
691   if (n_ops > 0) {
692     if (N->hasOneUse())
693       return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
694     else
695       return CurDAG->getTargetNode(NewOpc, OpVT, Ops, n_ops);
696   } else
697     return SelectCode(Op);
698 }
699
700 /// createPPCISelDag - This pass converts a legalized DAG into a 
701 /// SPU-specific DAG, ready for instruction scheduling.
702 ///
703 FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
704   return new SPUDAGToDAGISel(TM);
705 }