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