LLVM API Documentation

SPUISelDAGToDAG.cpp

Go to the documentation of this file.
00001 //===-- SPUISelDAGToDAG.cpp - CellSPU pattern matching inst selector ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a pattern matching instruction selector for the Cell SPU,
00011 // converting from a legalized dag to a SPU-target dag.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "SPU.h"
00016 #include "SPUTargetMachine.h"
00017 #include "SPUISelLowering.h"
00018 #include "SPUHazardRecognizers.h"
00019 #include "SPUFrameInfo.h"
00020 #include "SPURegisterNames.h"
00021 #include "SPUTargetMachine.h"
00022 #include "llvm/CodeGen/MachineConstantPool.h"
00023 #include "llvm/CodeGen/MachineInstrBuilder.h"
00024 #include "llvm/CodeGen/MachineFunction.h"
00025 #include "llvm/CodeGen/SelectionDAG.h"
00026 #include "llvm/CodeGen/SelectionDAGISel.h"
00027 #include "llvm/CodeGen/PseudoSourceValue.h"
00028 #include "llvm/Target/TargetOptions.h"
00029 #include "llvm/ADT/Statistic.h"
00030 #include "llvm/Constants.h"
00031 #include "llvm/GlobalValue.h"
00032 #include "llvm/Intrinsics.h"
00033 #include "llvm/LLVMContext.h"
00034 #include "llvm/Support/Debug.h"
00035 #include "llvm/Support/ErrorHandling.h"
00036 #include "llvm/Support/MathExtras.h"
00037 #include "llvm/Support/Compiler.h"
00038 #include "llvm/Support/raw_ostream.h"
00039 
00040 using namespace llvm;
00041 
00042 namespace {
00043   //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
00044   bool
00045   isI64IntS10Immediate(ConstantSDNode *CN)
00046   {
00047     return isS10Constant(CN->getSExtValue());
00048   }
00049 
00050   //! ConstantSDNode predicate for i32 sign-extended, 10-bit immediates
00051   bool
00052   isI32IntS10Immediate(ConstantSDNode *CN)
00053   {
00054     return isS10Constant(CN->getSExtValue());
00055   }
00056 
00057   //! ConstantSDNode predicate for i32 unsigned 10-bit immediate values
00058   bool
00059   isI32IntU10Immediate(ConstantSDNode *CN)
00060   {
00061     return isU10Constant(CN->getSExtValue());
00062   }
00063 
00064   //! ConstantSDNode predicate for i16 sign-extended, 10-bit immediate values
00065   bool
00066   isI16IntS10Immediate(ConstantSDNode *CN)
00067   {
00068     return isS10Constant(CN->getSExtValue());
00069   }
00070 
00071   //! SDNode predicate for i16 sign-extended, 10-bit immediate values
00072   bool
00073   isI16IntS10Immediate(SDNode *N)
00074   {
00075     ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
00076     return (CN != 0 && isI16IntS10Immediate(CN));
00077   }
00078 
00079   //! ConstantSDNode predicate for i16 unsigned 10-bit immediate values
00080   bool
00081   isI16IntU10Immediate(ConstantSDNode *CN)
00082   {
00083     return isU10Constant((short) CN->getZExtValue());
00084   }
00085 
00086   //! SDNode predicate for i16 sign-extended, 10-bit immediate values
00087   bool
00088   isI16IntU10Immediate(SDNode *N)
00089   {
00090     return (N->getOpcode() == ISD::Constant
00091             && isI16IntU10Immediate(cast<ConstantSDNode>(N)));
00092   }
00093 
00094   //! ConstantSDNode predicate for signed 16-bit values
00095   /*!
00096     \arg CN The constant SelectionDAG node holding the value
00097     \arg Imm The returned 16-bit value, if returning true
00098 
00099     This predicate tests the value in \a CN to see whether it can be
00100     represented as a 16-bit, sign-extended quantity. Returns true if
00101     this is the case.
00102    */
00103   bool
00104   isIntS16Immediate(ConstantSDNode *CN, short &Imm)
00105   {
00106     EVT vt = CN->getValueType(0);
00107     Imm = (short) CN->getZExtValue();
00108     if (vt.getSimpleVT() >= MVT::i1 && vt.getSimpleVT() <= MVT::i16) {
00109       return true;
00110     } else if (vt == MVT::i32) {
00111       int32_t i_val = (int32_t) CN->getZExtValue();
00112       short s_val = (short) i_val;
00113       return i_val == s_val;
00114     } else {
00115       int64_t i_val = (int64_t) CN->getZExtValue();
00116       short s_val = (short) i_val;
00117       return i_val == s_val;
00118     }
00119 
00120     return false;
00121   }
00122 
00123   //! SDNode predicate for signed 16-bit values.
00124   bool
00125   isIntS16Immediate(SDNode *N, short &Imm)
00126   {
00127     return (N->getOpcode() == ISD::Constant
00128             && isIntS16Immediate(cast<ConstantSDNode>(N), Imm));
00129   }
00130 
00131   //! ConstantFPSDNode predicate for representing floats as 16-bit sign ext.
00132   static bool
00133   isFPS16Immediate(ConstantFPSDNode *FPN, short &Imm)
00134   {
00135     EVT vt = FPN->getValueType(0);
00136     if (vt == MVT::f32) {
00137       int val = FloatToBits(FPN->getValueAPF().convertToFloat());
00138       int sval = (int) ((val << 16) >> 16);
00139       Imm = (short) val;
00140       return val == sval;
00141     }
00142 
00143     return false;
00144   }
00145 
00146   bool
00147   isHighLow(const SDValue &Op)
00148   {
00149     return (Op.getOpcode() == SPUISD::IndirectAddr
00150             && ((Op.getOperand(0).getOpcode() == SPUISD::Hi
00151                  && Op.getOperand(1).getOpcode() == SPUISD::Lo)
00152                 || (Op.getOperand(0).getOpcode() == SPUISD::Lo
00153                     && Op.getOperand(1).getOpcode() == SPUISD::Hi)));
00154   }
00155 
00156   //===------------------------------------------------------------------===//
00157   //! EVT to "useful stuff" mapping structure:
00158 
00159   struct valtype_map_s {
00160     EVT VT;
00161     unsigned ldresult_ins;      /// LDRESULT instruction (0 = undefined)
00162     bool ldresult_imm;          /// LDRESULT instruction requires immediate?
00163     unsigned lrinst;            /// LR instruction
00164   };
00165 
00166   const valtype_map_s valtype_map[] = {
00167     { MVT::i8,    SPU::ORBIr8,  true,  SPU::LRr8 },
00168     { MVT::i16,   SPU::ORHIr16, true,  SPU::LRr16 },
00169     { MVT::i32,   SPU::ORIr32,  true,  SPU::LRr32 },
00170     { MVT::i64,   SPU::ORr64,   false, SPU::LRr64 },
00171     { MVT::f32,   SPU::ORf32,   false, SPU::LRf32 },
00172     { MVT::f64,   SPU::ORf64,   false, SPU::LRf64 },
00173     // vector types... (sigh!)
00174     { MVT::v16i8, 0,            false, SPU::LRv16i8 },
00175     { MVT::v8i16, 0,            false, SPU::LRv8i16 },
00176     { MVT::v4i32, 0,            false, SPU::LRv4i32 },
00177     { MVT::v2i64, 0,            false, SPU::LRv2i64 },
00178     { MVT::v4f32, 0,            false, SPU::LRv4f32 },
00179     { MVT::v2f64, 0,            false, SPU::LRv2f64 }
00180   };
00181 
00182   const size_t n_valtype_map = sizeof(valtype_map) / sizeof(valtype_map[0]);
00183 
00184   const valtype_map_s *getValueTypeMapEntry(EVT VT)
00185   {
00186     const valtype_map_s *retval = 0;
00187     for (size_t i = 0; i < n_valtype_map; ++i) {
00188       if (valtype_map[i].VT == VT) {
00189         retval = valtype_map + i;
00190         break;
00191       }
00192     }
00193 
00194 
00195 #ifndef NDEBUG
00196     if (retval == 0) {
00197       std::string msg;
00198       raw_string_ostream Msg(msg);
00199       Msg << "SPUISelDAGToDAG.cpp: getValueTypeMapEntry returns NULL for "
00200            << VT.getEVTString();
00201       llvm_report_error(Msg.str());
00202     }
00203 #endif
00204 
00205     return retval;
00206   }
00207 
00208   //! Generate the carry-generate shuffle mask.
00209   SDValue getCarryGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
00210     SmallVector<SDValue, 16 > ShufBytes;
00211 
00212     // Create the shuffle mask for "rotating" the borrow up one register slot
00213     // once the borrow is generated.
00214     ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
00215     ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
00216     ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
00217     ShufBytes.push_back(DAG.getConstant(0x80808080, MVT::i32));
00218 
00219     return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
00220                        &ShufBytes[0], ShufBytes.size());
00221   }
00222 
00223   //! Generate the borrow-generate shuffle mask
00224   SDValue getBorrowGenerateShufMask(SelectionDAG &DAG, DebugLoc dl) {
00225     SmallVector<SDValue, 16 > ShufBytes;
00226 
00227     // Create the shuffle mask for "rotating" the borrow up one register slot
00228     // once the borrow is generated.
00229     ShufBytes.push_back(DAG.getConstant(0x04050607, MVT::i32));
00230     ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
00231     ShufBytes.push_back(DAG.getConstant(0x0c0d0e0f, MVT::i32));
00232     ShufBytes.push_back(DAG.getConstant(0xc0c0c0c0, MVT::i32));
00233 
00234     return DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
00235                        &ShufBytes[0], ShufBytes.size());
00236   }
00237 
00238   //===------------------------------------------------------------------===//
00239   /// SPUDAGToDAGISel - Cell SPU-specific code to select SPU machine
00240   /// instructions for SelectionDAG operations.
00241   ///
00242   class SPUDAGToDAGISel :
00243     public SelectionDAGISel
00244   {
00245     SPUTargetMachine &TM;
00246     SPUTargetLowering &SPUtli;
00247     unsigned GlobalBaseReg;
00248 
00249   public:
00250     explicit SPUDAGToDAGISel(SPUTargetMachine &tm) :
00251       SelectionDAGISel(tm),
00252       TM(tm),
00253       SPUtli(*tm.getTargetLowering())
00254     { }
00255 
00256     virtual bool runOnMachineFunction(MachineFunction &MF) {
00257       // Make sure we re-emit a set of the global base reg if necessary
00258       GlobalBaseReg = 0;
00259       SelectionDAGISel::runOnMachineFunction(MF);
00260       return true;
00261     }
00262 
00263     /// getI32Imm - Return a target constant with the specified value, of type
00264     /// i32.
00265     inline SDValue getI32Imm(uint32_t Imm) {
00266       return CurDAG->getTargetConstant(Imm, MVT::i32);
00267     }
00268 
00269     /// getI64Imm - Return a target constant with the specified value, of type
00270     /// i64.
00271     inline SDValue getI64Imm(uint64_t Imm) {
00272       return CurDAG->getTargetConstant(Imm, MVT::i64);
00273     }
00274 
00275     /// getSmallIPtrImm - Return a target constant of pointer type.
00276     inline SDValue getSmallIPtrImm(unsigned Imm) {
00277       return CurDAG->getTargetConstant(Imm, SPUtli.getPointerTy());
00278       }
00279 
00280     SDNode *emitBuildVector(SDValue build_vec) {
00281       EVT vecVT = build_vec.getValueType();
00282       EVT eltVT = vecVT.getVectorElementType();
00283       SDNode *bvNode = build_vec.getNode();
00284       DebugLoc dl = bvNode->getDebugLoc();
00285 
00286       // Check to see if this vector can be represented as a CellSPU immediate
00287       // constant by invoking all of the instruction selection predicates:
00288       if (((vecVT == MVT::v8i16) &&
00289            (SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i16).getNode() != 0)) ||
00290           ((vecVT == MVT::v4i32) &&
00291            ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
00292             (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
00293             (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i32).getNode() != 0) ||
00294             (SPU::get_v4i32_imm(bvNode, *CurDAG).getNode() != 0))) ||
00295           ((vecVT == MVT::v2i64) &&
00296            ((SPU::get_vec_i16imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
00297             (SPU::get_ILHUvec_imm(bvNode, *CurDAG, MVT::i64).getNode() != 0) ||
00298             (SPU::get_vec_u18imm(bvNode, *CurDAG, MVT::i64).getNode() != 0))))
00299         return Select(build_vec);
00300 
00301       // No, need to emit a constant pool spill:
00302       std::vector<Constant*> CV;
00303 
00304       for (size_t i = 0; i < build_vec.getNumOperands(); ++i) {
00305         ConstantSDNode *V = dyn_cast<ConstantSDNode > (build_vec.getOperand(i));
00306         CV.push_back(const_cast<ConstantInt *> (V->getConstantIntValue()));
00307       }
00308 
00309       Constant *CP = ConstantVector::get(CV);
00310       SDValue CPIdx = CurDAG->getConstantPool(CP, SPUtli.getPointerTy());
00311       unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
00312       SDValue CGPoolOffset =
00313               SPU::LowerConstantPool(CPIdx, *CurDAG,
00314                                      SPUtli.getSPUTargetMachine());
00315       return SelectCode(CurDAG->getLoad(build_vec.getValueType(), dl,
00316                                         CurDAG->getEntryNode(), CGPoolOffset,
00317                                         PseudoSourceValue::getConstantPool(), 0,
00318                                         false, Alignment));
00319     }
00320 
00321     /// Select - Convert the specified operand from a target-independent to a
00322     /// target-specific node if it hasn't already been changed.
00323     SDNode *Select(SDValue Op);
00324 
00325     //! Emit the instruction sequence for i64 shl
00326     SDNode *SelectSHLi64(SDValue &Op, EVT OpVT);
00327 
00328     //! Emit the instruction sequence for i64 srl
00329     SDNode *SelectSRLi64(SDValue &Op, EVT OpVT);
00330 
00331     //! Emit the instruction sequence for i64 sra
00332     SDNode *SelectSRAi64(SDValue &Op, EVT OpVT);
00333 
00334     //! Emit the necessary sequence for loading i64 constants:
00335     SDNode *SelectI64Constant(SDValue &Op, EVT OpVT, DebugLoc dl);
00336 
00337     //! Alternate instruction emit sequence for loading i64 constants
00338     SDNode *SelectI64Constant(uint64_t i64const, EVT OpVT, DebugLoc dl);
00339 
00340     //! Returns true if the address N is an A-form (local store) address
00341     bool SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
00342                          SDValue &Index);
00343 
00344     //! D-form address predicate
00345     bool SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
00346                          SDValue &Index);
00347 
00348     /// Alternate D-form address using i7 offset predicate
00349     bool SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
00350                           SDValue &Base);
00351 
00352     /// D-form address selection workhorse
00353     bool DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Disp,
00354                                SDValue &Base, int minOffset, int maxOffset);
00355 
00356     //! Address predicate if N can be expressed as an indexed [r+r] operation.
00357     bool SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
00358                          SDValue &Index);
00359 
00360     /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
00361     /// inline asm expressions.
00362     virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
00363                                               char ConstraintCode,
00364                                               std::vector<SDValue> &OutOps) {
00365       SDValue Op0, Op1;
00366       switch (ConstraintCode) {
00367       default: return true;
00368       case 'm':   // memory
00369         if (!SelectDFormAddr(Op, Op, Op0, Op1)
00370             && !SelectAFormAddr(Op, Op, Op0, Op1))
00371           SelectXFormAddr(Op, Op, Op0, Op1);
00372         break;
00373       case 'o':   // offsetable
00374         if (!SelectDFormAddr(Op, Op, Op0, Op1)
00375             && !SelectAFormAddr(Op, Op, Op0, Op1)) {
00376           Op0 = Op;
00377           Op1 = getSmallIPtrImm(0);
00378         }
00379         break;
00380       case 'v':   // not offsetable
00381 #if 1
00382         llvm_unreachable("InlineAsmMemoryOperand 'v' constraint not handled.");
00383 #else
00384         SelectAddrIdxOnly(Op, Op, Op0, Op1);
00385 #endif
00386         break;
00387       }
00388 
00389       OutOps.push_back(Op0);
00390       OutOps.push_back(Op1);
00391       return false;
00392     }
00393 
00394     /// InstructionSelect - This callback is invoked by
00395     /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
00396     virtual void InstructionSelect();
00397 
00398     virtual const char *getPassName() const {
00399       return "Cell SPU DAG->DAG Pattern Instruction Selection";
00400     }
00401 
00402     /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
00403     /// this target when scheduling the DAG.
00404     virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
00405       const TargetInstrInfo *II = TM.getInstrInfo();
00406       assert(II && "No InstrInfo?");
00407       return new SPUHazardRecognizer(*II);
00408     }
00409 
00410     // Include the pieces autogenerated from the target description.
00411 #include "SPUGenDAGISel.inc"
00412   };
00413 }
00414 
00415 /// InstructionSelect - This callback is invoked by
00416 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
00417 void
00418 SPUDAGToDAGISel::InstructionSelect()
00419 {
00420   DEBUG(BB->dump());
00421 
00422   // Select target instructions for the DAG.
00423   SelectRoot(*CurDAG);
00424   CurDAG->RemoveDeadNodes();
00425 }
00426 
00427 /*!
00428  \arg Op The ISD instruction operand
00429  \arg N The address to be tested
00430  \arg Base The base address
00431  \arg Index The base address index
00432  */
00433 bool
00434 SPUDAGToDAGISel::SelectAFormAddr(SDValue Op, SDValue N, SDValue &Base,
00435                     SDValue &Index) {
00436   // These match the addr256k operand type:
00437   EVT OffsVT = MVT::i16;
00438   SDValue Zero = CurDAG->getTargetConstant(0, OffsVT);
00439 
00440   switch (N.getOpcode()) {
00441   case ISD::Constant:
00442   case ISD::ConstantPool:
00443   case ISD::GlobalAddress:
00444     llvm_report_error("SPU SelectAFormAddr: Constant/Pool/Global not lowered.");
00445     /*NOTREACHED*/
00446 
00447   case ISD::TargetConstant:
00448   case ISD::TargetGlobalAddress:
00449   case ISD::TargetJumpTable:
00450     llvm_report_error("SPUSelectAFormAddr: Target Constant/Pool/Global "
00451                       "not wrapped as A-form address.");
00452     /*NOTREACHED*/
00453 
00454   case SPUISD::AFormAddr:
00455     // Just load from memory if there's only a single use of the location,
00456     // otherwise, this will get handled below with D-form offset addresses
00457     if (N.hasOneUse()) {
00458       SDValue Op0 = N.getOperand(0);
00459       switch (Op0.getOpcode()) {
00460       case ISD::TargetConstantPool:
00461       case ISD::TargetJumpTable:
00462         Base = Op0;
00463         Index = Zero;
00464         return true;
00465 
00466       case ISD::TargetGlobalAddress: {
00467         GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op0);
00468         GlobalValue *GV = GSDN->getGlobal();
00469         if (GV->getAlignment() == 16) {
00470           Base = Op0;
00471           Index = Zero;
00472           return true;
00473         }
00474         break;
00475       }
00476       }
00477     }
00478     break;
00479   }
00480   return false;
00481 }
00482 
00483 bool
00484 SPUDAGToDAGISel::SelectDForm2Addr(SDValue Op, SDValue N, SDValue &Disp,
00485                                   SDValue &Base) {
00486   const int minDForm2Offset = -(1 << 7);
00487   const int maxDForm2Offset = (1 << 7) - 1;
00488   return DFormAddressPredicate(Op, N, Disp, Base, minDForm2Offset,
00489                                maxDForm2Offset);
00490 }
00491 
00492 /*!
00493   \arg Op The ISD instruction (ignored)
00494   \arg N The address to be tested
00495   \arg Base Base address register/pointer
00496   \arg Index Base address index
00497 
00498   Examine the input address by a base register plus a signed 10-bit
00499   displacement, [r+I10] (D-form address).
00500 
00501   \return true if \a N is a D-form address with \a Base and \a Index set
00502   to non-empty SDValue instances.
00503 */
00504 bool
00505 SPUDAGToDAGISel::SelectDFormAddr(SDValue Op, SDValue N, SDValue &Base,
00506                                  SDValue &Index) {
00507   return DFormAddressPredicate(Op, N, Base, Index,
00508                                SPUFrameInfo::minFrameOffset(),
00509                                SPUFrameInfo::maxFrameOffset());
00510 }
00511 
00512 bool
00513 SPUDAGToDAGISel::DFormAddressPredicate(SDValue Op, SDValue N, SDValue &Base,
00514                                       SDValue &Index, int minOffset,
00515                                       int maxOffset) {
00516   unsigned Opc = N.getOpcode();
00517   EVT PtrTy = SPUtli.getPointerTy();
00518 
00519   if (Opc == ISD::FrameIndex) {
00520     // Stack frame index must be less than 512 (divided by 16):
00521     FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N);
00522     int FI = int(FIN->getIndex());
00523     DEBUG(cerr << "SelectDFormAddr: ISD::FrameIndex = "
00524                << FI << "\n");
00525     if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
00526       Base = CurDAG->getTargetConstant(0, PtrTy);
00527       Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
00528       return true;
00529     }
00530   } else if (Opc == ISD::ADD) {
00531     // Generated by getelementptr
00532     const SDValue Op0 = N.getOperand(0);
00533     const SDValue Op1 = N.getOperand(1);
00534 
00535     if ((Op0.getOpcode() == SPUISD::Hi && Op1.getOpcode() == SPUISD::Lo)
00536         || (Op1.getOpcode() == SPUISD::Hi && Op0.getOpcode() == SPUISD::Lo)) {
00537       Base = CurDAG->getTargetConstant(0, PtrTy);
00538       Index = N;
00539       return true;
00540     } else if (Op1.getOpcode() == ISD::Constant
00541                || Op1.getOpcode() == ISD::TargetConstant) {
00542       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op1);
00543       int32_t offset = int32_t(CN->getSExtValue());
00544 
00545       if (Op0.getOpcode() == ISD::FrameIndex) {
00546         FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op0);
00547         int FI = int(FIN->getIndex());
00548         DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
00549                    << " frame index = " << FI << "\n");
00550 
00551         if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
00552           Base = CurDAG->getTargetConstant(offset, PtrTy);
00553           Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
00554           return true;
00555         }
00556       } else if (offset > minOffset && offset < maxOffset) {
00557         Base = CurDAG->getTargetConstant(offset, PtrTy);
00558         Index = Op0;
00559         return true;
00560       }
00561     } else if (Op0.getOpcode() == ISD::Constant
00562                || Op0.getOpcode() == ISD::TargetConstant) {
00563       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0);
00564       int32_t offset = int32_t(CN->getSExtValue());
00565 
00566       if (Op1.getOpcode() == ISD::FrameIndex) {
00567         FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op1);
00568         int FI = int(FIN->getIndex());
00569         DEBUG(cerr << "SelectDFormAddr: ISD::ADD offset = " << offset
00570                    << " frame index = " << FI << "\n");
00571 
00572         if (SPUFrameInfo::FItoStackOffset(FI) < maxOffset) {
00573           Base = CurDAG->getTargetConstant(offset, PtrTy);
00574           Index = CurDAG->getTargetFrameIndex(FI, PtrTy);
00575           return true;
00576         }
00577       } else if (offset > minOffset && offset < maxOffset) {
00578         Base = CurDAG->getTargetConstant(offset, PtrTy);
00579         Index = Op1;
00580         return true;
00581       }
00582     }
00583   } else if (Opc == SPUISD::IndirectAddr) {
00584     // Indirect with constant offset -> D-Form address
00585     const SDValue Op0 = N.getOperand(0);
00586     const SDValue Op1 = N.getOperand(1);
00587 
00588     if (Op0.getOpcode() == SPUISD::Hi
00589         && Op1.getOpcode() == SPUISD::Lo) {
00590       // (SPUindirect (SPUhi <arg>, 0), (SPUlo <arg>, 0))
00591       Base = CurDAG->getTargetConstant(0, PtrTy);
00592       Index = N;
00593       return true;
00594     } else if (isa<ConstantSDNode>(Op0) || isa<ConstantSDNode>(Op1)) {
00595       int32_t offset = 0;
00596       SDValue idxOp;
00597 
00598       if (isa<ConstantSDNode>(Op1)) {
00599         ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
00600         offset = int32_t(CN->getSExtValue());
00601         idxOp = Op0;
00602       } else if (isa<ConstantSDNode>(Op0)) {
00603         ConstantSDNode *CN = cast<ConstantSDNode>(Op0);
00604         offset = int32_t(CN->getSExtValue());
00605         idxOp = Op1;
00606       }
00607 
00608       if (offset >= minOffset && offset <= maxOffset) {
00609         Base = CurDAG->getTargetConstant(offset, PtrTy);
00610         Index = idxOp;
00611         return true;
00612       }
00613     }
00614   } else if (Opc == SPUISD::AFormAddr) {
00615     Base = CurDAG->getTargetConstant(0, N.getValueType());
00616     Index = N;
00617     return true;
00618   } else if (Opc == SPUISD::LDRESULT) {
00619     Base = CurDAG->getTargetConstant(0, N.getValueType());
00620     Index = N;
00621     return true;
00622   } else if (Opc == ISD::Register || Opc == ISD::CopyFromReg) {
00623     unsigned OpOpc = Op.getOpcode();
00624 
00625     if (OpOpc == ISD::STORE || OpOpc == ISD::LOAD) {
00626       // Direct load/store without getelementptr
00627       SDValue Addr, Offs;
00628 
00629       // Get the register from CopyFromReg
00630       if (Opc == ISD::CopyFromReg)
00631         Addr = N.getOperand(1);
00632       else
00633         Addr = N;                       // Register
00634 
00635       Offs = ((OpOpc == ISD::STORE) ? Op.getOperand(3) : Op.getOperand(2));
00636 
00637       if (Offs.getOpcode() == ISD::Constant || Offs.getOpcode() == ISD::UNDEF) {
00638         if (Offs.getOpcode() == ISD::UNDEF)
00639           Offs = CurDAG->getTargetConstant(0, Offs.getValueType());
00640 
00641         Base = Offs;
00642         Index = Addr;
00643         return true;
00644       }
00645     } else {
00646       /* If otherwise unadorned, default to D-form address with 0 offset: */
00647       if (Opc == ISD::CopyFromReg) {
00648         Index = N.getOperand(1);
00649       } else {
00650         Index = N;
00651       }
00652 
00653       Base = CurDAG->getTargetConstant(0, Index.getValueType());
00654       return true;
00655     }
00656   }
00657 
00658   return false;
00659 }
00660 
00661 /*!
00662   \arg Op The ISD instruction operand
00663   \arg N The address operand
00664   \arg Base The base pointer operand
00665   \arg Index The offset/index operand
00666 
00667   If the address \a N can be expressed as an A-form or D-form address, returns
00668   false.  Otherwise, creates two operands, Base and Index that will become the
00669   (r)(r) X-form address.
00670 */
00671 bool
00672 SPUDAGToDAGISel::SelectXFormAddr(SDValue Op, SDValue N, SDValue &Base,
00673                                  SDValue &Index) {
00674   if (!SelectAFormAddr(Op, N, Base, Index)
00675       && !SelectDFormAddr(Op, N, Base, Index)) {
00676     // If the address is neither A-form or D-form, punt and use an X-form
00677     // address:
00678     Base = N.getOperand(1);
00679     Index = N.getOperand(0);
00680     return true;
00681   }
00682 
00683   return false;
00684 }
00685 
00686 //! Convert the operand from a target-independent to a target-specific node
00687 /*!
00688  */
00689 SDNode *
00690 SPUDAGToDAGISel::Select(SDValue Op) {
00691   SDNode *N = Op.getNode();
00692   unsigned Opc = N->getOpcode();
00693   int n_ops = -1;
00694   unsigned NewOpc;
00695   EVT OpVT = Op.getValueType();
00696   SDValue Ops[8];
00697   DebugLoc dl = N->getDebugLoc();
00698 
00699   if (N->isMachineOpcode()) {
00700     return NULL;   // Already selected.
00701   }
00702 
00703   if (Opc == ISD::FrameIndex) {
00704     int FI = cast<FrameIndexSDNode>(N)->getIndex();
00705     SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
00706     SDValue Imm0 = CurDAG->getTargetConstant(0, Op.getValueType());
00707 
00708     if (FI < 128) {
00709       NewOpc = SPU::AIr32;
00710       Ops[0] = TFI;
00711       Ops[1] = Imm0;
00712       n_ops = 2;
00713     } else {
00714       NewOpc = SPU::Ar32;
00715       Ops[0] = CurDAG->getRegister(SPU::R1, Op.getValueType());
00716       Ops[1] = SDValue(CurDAG->getTargetNode(SPU::ILAr32, dl, Op.getValueType(),
00717                                              TFI, Imm0), 0);
00718       n_ops = 2;
00719     }
00720   } else if (Opc == ISD::Constant && OpVT == MVT::i64) {
00721     // Catch the i64 constants that end up here. Note: The backend doesn't
00722     // attempt to legalize the constant (it's useless because DAGCombiner
00723     // will insert 64-bit constants and we can't stop it).
00724     return SelectI64Constant(Op, OpVT, Op.getDebugLoc());
00725   } else if ((Opc == ISD::ZERO_EXTEND || Opc == ISD::ANY_EXTEND)
00726              && OpVT == MVT::i64) {
00727     SDValue Op0 = Op.getOperand(0);
00728     EVT Op0VT = Op0.getValueType();
00729     EVT Op0VecVT = EVT::getVectorVT(*CurDAG->getContext(),
00730                                     Op0VT, (128 / Op0VT.getSizeInBits()));
00731     EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), 
00732                                    OpVT, (128 / OpVT.getSizeInBits()));
00733     SDValue shufMask;
00734 
00735     switch (Op0VT.getSimpleVT().SimpleTy) {
00736     default:
00737       llvm_report_error("CellSPU Select: Unhandled zero/any extend EVT");
00738       /*NOTREACHED*/
00739     case MVT::i32:
00740       shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
00741                                  CurDAG->getConstant(0x80808080, MVT::i32),
00742                                  CurDAG->getConstant(0x00010203, MVT::i32),
00743                                  CurDAG->getConstant(0x80808080, MVT::i32),
00744                                  CurDAG->getConstant(0x08090a0b, MVT::i32));
00745       break;
00746 
00747     case MVT::i16:
00748       shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
00749                                  CurDAG->getConstant(0x80808080, MVT::i32),
00750                                  CurDAG->getConstant(0x80800203, MVT::i32),
00751                                  CurDAG->getConstant(0x80808080, MVT::i32),
00752                                  CurDAG->getConstant(0x80800a0b, MVT::i32));
00753       break;
00754 
00755     case MVT::i8:
00756       shufMask = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
00757                                  CurDAG->getConstant(0x80808080, MVT::i32),
00758                                  CurDAG->getConstant(0x80808003, MVT::i32),
00759                                  CurDAG->getConstant(0x80808080, MVT::i32),
00760                                  CurDAG->getConstant(0x8080800b, MVT::i32));
00761       break;
00762     }
00763 
00764     SDNode *shufMaskLoad = emitBuildVector(shufMask);
00765     SDNode *PromoteScalar =
00766             SelectCode(CurDAG->getNode(SPUISD::PREFSLOT2VEC, dl, Op0VecVT, Op0));
00767 
00768     SDValue zextShuffle =
00769             CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
00770                             SDValue(PromoteScalar, 0),
00771                             SDValue(PromoteScalar, 0),
00772                             SDValue(shufMaskLoad, 0));
00773 
00774     // N.B.: BIT_CONVERT replaces and updates the zextShuffle node, so we
00775     // re-use it in the VEC2PREFSLOT selection without needing to explicitly
00776     // call SelectCode (it's already done for us.)
00777     SelectCode(CurDAG->getNode(ISD::BIT_CONVERT, dl, OpVecVT, zextShuffle));
00778     return SelectCode(CurDAG->getNode(SPUISD::VEC2PREFSLOT, dl, OpVT,
00779                                       zextShuffle));
00780   } else if (Opc == ISD::ADD && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
00781     SDNode *CGLoad =
00782             emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl));
00783 
00784     return SelectCode(CurDAG->getNode(SPUISD::ADD64_MARKER, dl, OpVT,
00785                                       Op.getOperand(0), Op.getOperand(1),
00786                                       SDValue(CGLoad, 0)));
00787   } else if (Opc == ISD::SUB && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
00788     SDNode *CGLoad =
00789             emitBuildVector(getBorrowGenerateShufMask(*CurDAG, dl));
00790 
00791     return SelectCode(CurDAG->getNode(SPUISD::SUB64_MARKER, dl, OpVT,
00792                                       Op.getOperand(0), Op.getOperand(1),
00793                                       SDValue(CGLoad, 0)));
00794   } else if (Opc == ISD::MUL && (OpVT == MVT::i64 || OpVT == MVT::v2i64)) {
00795     SDNode *CGLoad =
00796             emitBuildVector(getCarryGenerateShufMask(*CurDAG, dl));
00797 
00798     return SelectCode(CurDAG->getNode(SPUISD::MUL64_MARKER, dl, OpVT,
00799                                       Op.getOperand(0), Op.getOperand(1),
00800                                       SDValue(CGLoad, 0)));
00801   } else if (Opc == ISD::TRUNCATE) {
00802     SDValue Op0 = Op.getOperand(0);
00803     if ((Op0.getOpcode() == ISD::SRA || Op0.getOpcode() == ISD::SRL)
00804         && OpVT == MVT::i32
00805         && Op0.getValueType() == MVT::i64) {
00806       // Catch (truncate:i32 ([sra|srl]:i64 arg, c), where c >= 32
00807       //
00808       // Take advantage of the fact that the upper 32 bits are in the
00809       // i32 preferred slot and avoid shuffle gymnastics:
00810       ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
00811       if (CN != 0) {
00812         unsigned shift_amt = unsigned(CN->getZExtValue());
00813 
00814         if (shift_amt >= 32) {
00815           SDNode *hi32 =
00816                   CurDAG->getTargetNode(SPU::ORr32_r64, dl, OpVT,
00817                                         Op0.getOperand(0));
00818 
00819           shift_amt -= 32;
00820           if (shift_amt > 0) {
00821             // Take care of the additional shift, if present:
00822             SDValue shift = CurDAG->getTargetConstant(shift_amt, MVT::i32);
00823             unsigned Opc = SPU::ROTMAIr32_i32;
00824 
00825             if (Op0.getOpcode() == ISD::SRL)
00826               Opc = SPU::ROTMr32;
00827 
00828             hi32 = CurDAG->getTargetNode(Opc, dl, OpVT, SDValue(hi32, 0),
00829                                          shift);
00830           }
00831 
00832           return hi32;
00833         }
00834       }
00835     }
00836   } else if (Opc == ISD::SHL) {
00837     if (OpVT == MVT::i64) {
00838       return SelectSHLi64(Op, OpVT);
00839     }
00840   } else if (Opc == ISD::SRL) {
00841     if (OpVT == MVT::i64) {
00842       return SelectSRLi64(Op, OpVT);
00843     }
00844   } else if (Opc == ISD::SRA) {
00845     if (OpVT == MVT::i64) {
00846       return SelectSRAi64(Op, OpVT);
00847     }
00848   } else if (Opc == ISD::FNEG
00849              && (OpVT == MVT::f64 || OpVT == MVT::v2f64)) {
00850     DebugLoc dl = Op.getDebugLoc();
00851     // Check if the pattern is a special form of DFNMS:
00852     // (fneg (fsub (fmul R64FP:$rA, R64FP:$rB), R64FP:$rC))
00853     SDValue Op0 = Op.getOperand(0);
00854     if (Op0.getOpcode() == ISD::FSUB) {
00855       SDValue Op00 = Op0.getOperand(0);
00856       if (Op00.getOpcode() == ISD::FMUL) {
00857         unsigned Opc = SPU::DFNMSf64;
00858         if (OpVT == MVT::v2f64)
00859           Opc = SPU::DFNMSv2f64;
00860 
00861         return CurDAG->getTargetNode(Opc, dl, OpVT,
00862                                      Op00.getOperand(0),
00863                                      Op00.getOperand(1),
00864                                      Op0.getOperand(1));
00865       }
00866     }
00867 
00868     SDValue negConst = CurDAG->getConstant(0x8000000000000000ULL, MVT::i64);
00869     SDNode *signMask = 0;
00870     unsigned Opc = SPU::XORfneg64;
00871 
00872     if (OpVT == MVT::f64) {
00873       signMask = SelectI64Constant(negConst, MVT::i64, dl);
00874     } else if (OpVT == MVT::v2f64) {
00875       Opc = SPU::XORfnegvec;
00876       signMask = emitBuildVector(CurDAG->getNode(ISD::BUILD_VECTOR, dl,
00877                                                  MVT::v2i64,
00878                                                  negConst, negConst));
00879     }
00880 
00881     return CurDAG->getTargetNode(Opc, dl, OpVT,
00882                                  Op.getOperand(0), SDValue(signMask, 0));
00883   } else if (Opc == ISD::FABS) {
00884     if (OpVT == MVT::f64) {
00885       SDNode *signMask = SelectI64Constant(0x7fffffffffffffffULL, MVT::i64, dl);
00886       return CurDAG->getTargetNode(SPU::ANDfabs64, dl, OpVT,
00887                                    Op.getOperand(0), SDValue(signMask, 0));
00888     } else if (OpVT == MVT::v2f64) {
00889       SDValue absConst = CurDAG->getConstant(0x7fffffffffffffffULL, MVT::i64);
00890       SDValue absVec = CurDAG->getNode(ISD::BUILD_VECTOR, dl, MVT::v2i64,
00891                                        absConst, absConst);
00892       SDNode *signMask = emitBuildVector(absVec);
00893       return CurDAG->getTargetNode(SPU::ANDfabsvec, dl, OpVT,
00894                                    Op.getOperand(0), SDValue(signMask, 0));
00895     }
00896   } else if (Opc == SPUISD::LDRESULT) {
00897     // Custom select instructions for LDRESULT
00898     EVT VT = N->getValueType(0);
00899     SDValue Arg = N->getOperand(0);
00900     SDValue Chain = N->getOperand(1);
00901     SDNode *Result;
00902     const valtype_map_s *vtm = getValueTypeMapEntry(VT);
00903 
00904     if (vtm->ldresult_ins == 0) {
00905       std::string msg;
00906       raw_string_ostream Msg(msg);
00907       Msg << "LDRESULT for unsupported type: "
00908            << VT.getEVTString();
00909       llvm_report_error(Msg.str());
00910     }
00911 
00912     Opc = vtm->ldresult_ins;
00913     if (vtm->ldresult_imm) {
00914       SDValue Zero = CurDAG->getTargetConstant(0, VT);
00915 
00916       Result = CurDAG->getTargetNode(Opc, dl, VT, MVT::Other, Arg, Zero, Chain);
00917     } else {
00918       Result = CurDAG->getTargetNode(Opc, dl, VT, MVT::Other, Arg, Arg, Chain);
00919     }
00920 
00921     return Result;
00922   } else if (Opc == SPUISD::IndirectAddr) {
00923     // Look at the operands: SelectCode() will catch the cases that aren't
00924     // specifically handled here.
00925     //
00926     // SPUInstrInfo catches the following patterns:
00927     // (SPUindirect (SPUhi ...), (SPUlo ...))
00928     // (SPUindirect $sp, imm)
00929     EVT VT = Op.getValueType();
00930     SDValue Op0 = N->getOperand(0);
00931     SDValue Op1 = N->getOperand(1);
00932     RegisterSDNode *RN;
00933 
00934     if ((Op0.getOpcode() != SPUISD::Hi && Op1.getOpcode() != SPUISD::Lo)
00935         || (Op0.getOpcode() == ISD::Register
00936             && ((RN = dyn_cast<RegisterSDNode>(Op0.getNode())) != 0
00937                 && RN->getReg() != SPU::R1))) {
00938       NewOpc = SPU::Ar32;
00939       if (Op1.getOpcode() == ISD::Constant) {
00940         ConstantSDNode *CN = cast<ConstantSDNode>(Op1);
00941         Op1 = CurDAG->getTargetConstant(CN->getSExtValue(), VT);
00942         NewOpc = (isI32IntS10Immediate(CN) ? SPU::AIr32 : SPU::Ar32);
00943       }
00944       Ops[0] = Op0;
00945       Ops[1] = Op1;
00946       n_ops = 2;
00947     }
00948   }
00949 
00950   if (n_ops > 0) {
00951     if (N->hasOneUse())
00952       return CurDAG->SelectNodeTo(N, NewOpc, OpVT, Ops, n_ops);
00953     else
00954       return CurDAG->getTargetNode(NewOpc, dl, OpVT, Ops, n_ops);
00955   } else
00956     return SelectCode(Op);
00957 }
00958 
00959 /*!
00960  * Emit the instruction sequence for i64 left shifts. The basic algorithm
00961  * is to fill the bottom two word slots with zeros so that zeros are shifted
00962  * in as the entire quadword is shifted left.
00963  *
00964  * \note This code could also be used to implement v2i64 shl.
00965  *
00966  * @param Op The shl operand
00967  * @param OpVT Op's machine value value type (doesn't need to be passed, but
00968  * makes life easier.)
00969  * @return The SDNode with the entire instruction sequence
00970  */
00971 SDNode *
00972 SPUDAGToDAGISel::SelectSHLi64(SDValue &Op, EVT OpVT) {
00973   SDValue Op0 = Op.getOperand(0);
00974   EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(), 
00975                                OpVT, (128 / OpVT.getSizeInBits()));
00976   SDValue ShiftAmt = Op.getOperand(1);
00977   EVT ShiftAmtVT = ShiftAmt.getValueType();
00978   SDNode *VecOp0, *SelMask, *ZeroFill, *Shift = 0;
00979   SDValue SelMaskVal;
00980   DebugLoc dl = Op.getDebugLoc();
00981 
00982   VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
00983   SelMaskVal = CurDAG->getTargetConstant(0xff00ULL, MVT::i16);
00984   SelMask = CurDAG->getTargetNode(SPU::FSMBIv2i64, dl, VecVT, SelMaskVal);
00985   ZeroFill = CurDAG->getTargetNode(SPU::ILv2i64, dl, VecVT,
00986                                    CurDAG->getTargetConstant(0, OpVT));
00987   VecOp0 = CurDAG->getTargetNode(SPU::SELBv2i64, dl, VecVT,
00988                                  SDValue(ZeroFill, 0),
00989                                  SDValue(VecOp0, 0),
00990                                  SDValue(SelMask, 0));
00991 
00992   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
00993     unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
00994     unsigned bits = unsigned(CN->getZExtValue()) & 7;
00995 
00996     if (bytes > 0) {
00997       Shift =
00998         CurDAG->getTargetNode(SPU::SHLQBYIv2i64, dl, VecVT,
00999                               SDValue(VecOp0, 0),
01000                               CurDAG->getTargetConstant(bytes, ShiftAmtVT));
01001     }
01002 
01003     if (bits > 0) {
01004       Shift =
01005         CurDAG->getTargetNode(SPU::SHLQBIIv2i64, dl, VecVT,
01006                               SDValue((Shift != 0 ? Shift : VecOp0), 0),
01007                               CurDAG->getTargetConstant(bits, ShiftAmtVT));
01008     }
01009   } else {
01010     SDNode *Bytes =
01011       CurDAG->getTargetNode(SPU::ROTMIr32, dl, ShiftAmtVT,
01012                             ShiftAmt,
01013                             CurDAG->getTargetConstant(3, ShiftAmtVT));
01014     SDNode *Bits =
01015       CurDAG->getTargetNode(SPU::ANDIr32, dl, ShiftAmtVT,
01016                             ShiftAmt,
01017                             CurDAG->getTargetConstant(7, ShiftAmtVT));
01018     Shift =
01019       CurDAG->getTargetNode(SPU::SHLQBYv2i64, dl, VecVT,
01020                             SDValue(VecOp0, 0), SDValue(Bytes, 0));
01021     Shift =
01022       CurDAG->getTargetNode(SPU::SHLQBIv2i64, dl, VecVT,
01023                             SDValue(Shift, 0), SDValue(Bits, 0));
01024   }
01025 
01026   return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
01027 }
01028 
01029 /*!
01030  * Emit the instruction sequence for i64 logical right shifts.
01031  *
01032  * @param Op The shl operand
01033  * @param OpVT Op's machine value value type (doesn't need to be passed, but
01034  * makes life easier.)
01035  * @return The SDNode with the entire instruction sequence
01036  */
01037 SDNode *
01038 SPUDAGToDAGISel::SelectSRLi64(SDValue &Op, EVT OpVT) {
01039   SDValue Op0 = Op.getOperand(0);
01040   EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(),
01041                                OpVT, (128 / OpVT.getSizeInBits()));
01042   SDValue ShiftAmt = Op.getOperand(1);
01043   EVT ShiftAmtVT = ShiftAmt.getValueType();
01044   SDNode *VecOp0, *Shift = 0;
01045   DebugLoc dl = Op.getDebugLoc();
01046 
01047   VecOp0 = CurDAG->getTargetNode(SPU::ORv2i64_i64, dl, VecVT, Op0);
01048 
01049   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
01050     unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
01051     unsigned bits = unsigned(CN->getZExtValue()) & 7;
01052 
01053     if (bytes > 0) {
01054       Shift =
01055         CurDAG->getTargetNode(SPU::ROTQMBYIv2i64, dl, VecVT,
01056                               SDValue(VecOp0, 0),
01057                               CurDAG->getTargetConstant(bytes, ShiftAmtVT));
01058     }
01059 
01060     if (bits > 0) {
01061       Shift =
01062         CurDAG->getTargetNode(SPU::ROTQMBIIv2i64, dl, VecVT,
01063                               SDValue((Shift != 0 ? Shift : VecOp0), 0),
01064                               CurDAG->getTargetConstant(bits, ShiftAmtVT));
01065     }
01066   } else {
01067     SDNode *Bytes =
01068       CurDAG->getTargetNode(SPU::ROTMIr32, dl, ShiftAmtVT,
01069                             ShiftAmt,
01070                             CurDAG->getTargetConstant(3, ShiftAmtVT));
01071     SDNode *Bits =
01072       CurDAG->getTargetNode(SPU::ANDIr32, dl, ShiftAmtVT,
01073                             ShiftAmt,
01074                             CurDAG->getTargetConstant(7, ShiftAmtVT));
01075 
01076     // Ensure that the shift amounts are negated!
01077     Bytes = CurDAG->getTargetNode(SPU::SFIr32, dl, ShiftAmtVT,
01078                                   SDValue(Bytes, 0),
01079                                   CurDAG->getTargetConstant(0, ShiftAmtVT));
01080 
01081     Bits = CurDAG->getTargetNode(SPU::SFIr32, dl, ShiftAmtVT,
01082                                  SDValue(Bits, 0),
01083                                  CurDAG->getTargetConstant(0, ShiftAmtVT));
01084 
01085     Shift =
01086       CurDAG->getTargetNode(SPU::ROTQMBYv2i64, dl, VecVT,
01087                             SDValue(VecOp0, 0), SDValue(Bytes, 0));
01088     Shift =
01089       CurDAG->getTargetNode(SPU::ROTQMBIv2i64, dl, VecVT,
01090                             SDValue(Shift, 0), SDValue(Bits, 0));
01091   }
01092 
01093   return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
01094 }
01095 
01096 /*!
01097  * Emit the instruction sequence for i64 arithmetic right shifts.
01098  *
01099  * @param Op The shl operand
01100  * @param OpVT Op's machine value value type (doesn't need to be passed, but
01101  * makes life easier.)
01102  * @return The SDNode with the entire instruction sequence
01103  */
01104 SDNode *
01105 SPUDAGToDAGISel::SelectSRAi64(SDValue &Op, EVT OpVT) {
01106   // Promote Op0 to vector
01107   EVT VecVT = EVT::getVectorVT(*CurDAG->getContext(), 
01108                                OpVT, (128 / OpVT.getSizeInBits()));
01109   SDValue ShiftAmt = Op.getOperand(1);
01110   EVT ShiftAmtVT = ShiftAmt.getValueType();
01111   DebugLoc dl = Op.getDebugLoc();
01112 
01113   SDNode *VecOp0 =
01114     CurDAG->getTargetNode(SPU::ORv2i64_i64, dl, VecVT, Op.getOperand(0));
01115 
01116   SDValue SignRotAmt = CurDAG->getTargetConstant(31, ShiftAmtVT);
01117   SDNode *SignRot =
01118     CurDAG->getTargetNode(SPU::ROTMAIv2i64_i32, dl, MVT::v2i64,
01119                           SDValue(VecOp0, 0), SignRotAmt);
01120   SDNode *UpperHalfSign =
01121     CurDAG->getTargetNode(SPU::ORi32_v4i32, dl, MVT::i32, SDValue(SignRot, 0));
01122 
01123   SDNode *UpperHalfSignMask =
01124     CurDAG->getTargetNode(SPU::FSM64r32, dl, VecVT, SDValue(UpperHalfSign, 0));
01125   SDNode *UpperLowerMask =
01126     CurDAG->getTargetNode(SPU::FSMBIv2i64, dl, VecVT,
01127                           CurDAG->getTargetConstant(0xff00ULL, MVT::i16));
01128   SDNode *UpperLowerSelect =
01129     CurDAG->getTargetNode(SPU::SELBv2i64, dl, VecVT,
01130                           SDValue(UpperHalfSignMask, 0),
01131                           SDValue(VecOp0, 0),
01132                           SDValue(UpperLowerMask, 0));
01133 
01134   SDNode *Shift = 0;
01135 
01136   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(ShiftAmt)) {
01137     unsigned bytes = unsigned(CN->getZExtValue()) >> 3;
01138     unsigned bits = unsigned(CN->getZExtValue()) & 7;
01139 
01140     if (bytes > 0) {
01141       bytes = 31 - bytes;
01142       Shift =
01143         CurDAG->getTargetNode(SPU::ROTQBYIv2i64, dl, VecVT,
01144                               SDValue(UpperLowerSelect, 0),
01145                               CurDAG->getTargetConstant(bytes, ShiftAmtVT));
01146     }
01147 
01148     if (bits > 0) {
01149       bits = 8 - bits;
01150       Shift =
01151         CurDAG->getTargetNode(SPU::ROTQBIIv2i64, dl, VecVT,
01152                               SDValue((Shift != 0 ? Shift : UpperLowerSelect), 0),
01153                               CurDAG->getTargetConstant(bits, ShiftAmtVT));
01154     }
01155   } else {
01156     SDNode *NegShift =
01157       CurDAG->getTargetNode(SPU::SFIr32, dl, ShiftAmtVT,
01158                             ShiftAmt, CurDAG->getTargetConstant(0, ShiftAmtVT));
01159 
01160     Shift =
01161       CurDAG->getTargetNode(SPU::ROTQBYBIv2i64_r32, dl, VecVT,
01162                             SDValue(UpperLowerSelect, 0), SDValue(NegShift, 0));
01163     Shift =
01164       CurDAG->getTargetNode(SPU::ROTQBIv2i64, dl, VecVT,
01165                             SDValue(Shift, 0), SDValue(NegShift, 0));
01166   }
01167 
01168   return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT, SDValue(Shift, 0));
01169 }
01170 
01171 /*!
01172  Do the necessary magic necessary to load a i64 constant
01173  */
01174 SDNode *SPUDAGToDAGISel::SelectI64Constant(SDValue& Op, EVT OpVT,
01175                                            DebugLoc dl) {
01176   ConstantSDNode *CN = cast<ConstantSDNode>(Op.getNode());
01177   return SelectI64Constant(CN->getZExtValue(), OpVT, dl);
01178 }
01179 
01180 SDNode *SPUDAGToDAGISel::SelectI64Constant(uint64_t Value64, EVT OpVT,
01181                                            DebugLoc dl) {
01182   EVT OpVecVT = EVT::getVectorVT(*CurDAG->getContext(), OpVT, 2);
01183   SDValue i64vec =
01184           SPU::LowerV2I64Splat(OpVecVT, *CurDAG, Value64, dl);
01185 
01186   // Here's where it gets interesting, because we have to parse out the
01187   // subtree handed back in i64vec:
01188 
01189   if (i64vec.getOpcode() == ISD::BIT_CONVERT) {
01190     // The degenerate case where the upper and lower bits in the splat are
01191     // identical:
01192     SDValue Op0 = i64vec.getOperand(0);
01193 
01194     ReplaceUses(i64vec, Op0);
01195     return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT,
01196                                  SDValue(emitBuildVector(Op0), 0));
01197   } else if (i64vec.getOpcode() == SPUISD::SHUFB) {
01198     SDValue lhs = i64vec.getOperand(0);
01199     SDValue rhs = i64vec.getOperand(1);
01200     SDValue shufmask = i64vec.getOperand(2);
01201 
01202     if (lhs.getOpcode() == ISD::BIT_CONVERT) {
01203       ReplaceUses(lhs, lhs.getOperand(0));
01204       lhs = lhs.getOperand(0);
01205     }
01206 
01207     SDNode *lhsNode = (lhs.getNode()->isMachineOpcode()
01208                        ? lhs.getNode()
01209                        : emitBuildVector(lhs));
01210 
01211     if (rhs.getOpcode() == ISD::BIT_CONVERT) {
01212       ReplaceUses(rhs, rhs.getOperand(0));
01213       rhs = rhs.getOperand(0);
01214     }
01215 
01216     SDNode *rhsNode = (rhs.getNode()->isMachineOpcode()
01217                        ? rhs.getNode()
01218                        : emitBuildVector(rhs));
01219 
01220     if (shufmask.getOpcode() == ISD::BIT_CONVERT) {
01221       ReplaceUses(shufmask, shufmask.getOperand(0));
01222       shufmask = shufmask.getOperand(0);
01223     }
01224 
01225     SDNode *shufMaskNode = (shufmask.getNode()->isMachineOpcode()
01226                             ? shufmask.getNode()
01227                             : emitBuildVector(shufmask));
01228 
01229     SDNode *shufNode =
01230             Select(CurDAG->getNode(SPUISD::SHUFB, dl, OpVecVT,
01231                                    SDValue(lhsNode, 0), SDValue(rhsNode, 0),
01232                                    SDValue(shufMaskNode, 0)));
01233 
01234     return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT,
01235                                  SDValue(shufNode, 0));
01236   } else if (i64vec.getOpcode() == ISD::BUILD_VECTOR) {
01237     return CurDAG->getTargetNode(SPU::ORi64_v2i64, dl, OpVT,
01238                                  SDValue(emitBuildVector(i64vec), 0));
01239   } else {
01240     llvm_report_error("SPUDAGToDAGISel::SelectI64Constant: Unhandled i64vec"
01241                       "condition");
01242   }
01243 }
01244 
01245 /// createSPUISelDag - This pass converts a legalized DAG into a
01246 /// SPU-specific DAG, ready for instruction scheduling.
01247 ///
01248 FunctionPass *llvm::createSPUISelDag(SPUTargetMachine &TM) {
01249   return new SPUDAGToDAGISel(TM);
01250 }