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