LLVM API Documentation

PPCISelLowering.cpp

Go to the documentation of this file.
00001 //===-- PPCISelLowering.cpp - PPC DAG Lowering Implementation -------------===//
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 implements the PPCISelLowering class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "PPCISelLowering.h"
00015 #include "PPCMachineFunctionInfo.h"
00016 #include "PPCPredicates.h"
00017 #include "PPCTargetMachine.h"
00018 #include "PPCPerfectShuffle.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/ADT/VectorExtras.h"
00021 #include "llvm/CodeGen/CallingConvLower.h"
00022 #include "llvm/CodeGen/MachineFrameInfo.h"
00023 #include "llvm/CodeGen/MachineFunction.h"
00024 #include "llvm/CodeGen/MachineInstrBuilder.h"
00025 #include "llvm/CodeGen/MachineRegisterInfo.h"
00026 #include "llvm/CodeGen/PseudoSourceValue.h"
00027 #include "llvm/CodeGen/SelectionDAG.h"
00028 #include "llvm/CallingConv.h"
00029 #include "llvm/Constants.h"
00030 #include "llvm/Function.h"
00031 #include "llvm/Intrinsics.h"
00032 #include "llvm/Support/MathExtras.h"
00033 #include "llvm/Target/TargetOptions.h"
00034 #include "llvm/Target/TargetLoweringObjectFile.h"
00035 #include "llvm/Support/CommandLine.h"
00036 #include "llvm/Support/ErrorHandling.h"
00037 #include "llvm/Support/raw_ostream.h"
00038 #include "llvm/DerivedTypes.h"
00039 using namespace llvm;
00040 
00041 static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
00042                                      CCValAssign::LocInfo &LocInfo,
00043                                      ISD::ArgFlagsTy &ArgFlags,
00044                                      CCState &State);
00045 static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, EVT &ValVT,
00046                                             EVT &LocVT,
00047                                             CCValAssign::LocInfo &LocInfo,
00048                                             ISD::ArgFlagsTy &ArgFlags,
00049                                             CCState &State);
00050 static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, EVT &ValVT,
00051                                               EVT &LocVT,
00052                                               CCValAssign::LocInfo &LocInfo,
00053                                               ISD::ArgFlagsTy &ArgFlags,
00054                                               CCState &State);
00055 
00056 static cl::opt<bool> EnablePPCPreinc("enable-ppc-preinc",
00057 cl::desc("enable preincrement load/store generation on PPC (experimental)"),
00058                                      cl::Hidden);
00059 
00060 static TargetLoweringObjectFile *CreateTLOF(const PPCTargetMachine &TM) {
00061   if (TM.getSubtargetImpl()->isDarwin())
00062     return new TargetLoweringObjectFileMachO();
00063   return new TargetLoweringObjectFileELF();
00064 }
00065 
00066 
00067 PPCTargetLowering::PPCTargetLowering(PPCTargetMachine &TM)
00068   : TargetLowering(TM, CreateTLOF(TM)), PPCSubTarget(*TM.getSubtargetImpl()) {
00069 
00070   setPow2DivIsCheap();
00071 
00072   // Use _setjmp/_longjmp instead of setjmp/longjmp.
00073   setUseUnderscoreSetJmp(true);
00074   setUseUnderscoreLongJmp(true);
00075 
00076   // Set up the register classes.
00077   addRegisterClass(MVT::i32, PPC::GPRCRegisterClass);
00078   addRegisterClass(MVT::f32, PPC::F4RCRegisterClass);
00079   addRegisterClass(MVT::f64, PPC::F8RCRegisterClass);
00080 
00081   // PowerPC has an i16 but no i8 (or i1) SEXTLOAD
00082   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
00083   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
00084 
00085   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
00086 
00087   // PowerPC has pre-inc load and store's.
00088   setIndexedLoadAction(ISD::PRE_INC, MVT::i1, Legal);
00089   setIndexedLoadAction(ISD::PRE_INC, MVT::i8, Legal);
00090   setIndexedLoadAction(ISD::PRE_INC, MVT::i16, Legal);
00091   setIndexedLoadAction(ISD::PRE_INC, MVT::i32, Legal);
00092   setIndexedLoadAction(ISD::PRE_INC, MVT::i64, Legal);
00093   setIndexedStoreAction(ISD::PRE_INC, MVT::i1, Legal);
00094   setIndexedStoreAction(ISD::PRE_INC, MVT::i8, Legal);
00095   setIndexedStoreAction(ISD::PRE_INC, MVT::i16, Legal);
00096   setIndexedStoreAction(ISD::PRE_INC, MVT::i32, Legal);
00097   setIndexedStoreAction(ISD::PRE_INC, MVT::i64, Legal);
00098 
00099   // This is used in the ppcf128->int sequence.  Note it has different semantics
00100   // from FP_ROUND:  that rounds to nearest, this rounds to zero.
00101   setOperationAction(ISD::FP_ROUND_INREG, MVT::ppcf128, Custom);
00102 
00103   // PowerPC has no SREM/UREM instructions
00104   setOperationAction(ISD::SREM, MVT::i32, Expand);
00105   setOperationAction(ISD::UREM, MVT::i32, Expand);
00106   setOperationAction(ISD::SREM, MVT::i64, Expand);
00107   setOperationAction(ISD::UREM, MVT::i64, Expand);
00108 
00109   // Don't use SMUL_LOHI/UMUL_LOHI or SDIVREM/UDIVREM to lower SREM/UREM.
00110   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
00111   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
00112   setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
00113   setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
00114   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
00115   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
00116   setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
00117   setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
00118 
00119   // We don't support sin/cos/sqrt/fmod/pow
00120   setOperationAction(ISD::FSIN , MVT::f64, Expand);
00121   setOperationAction(ISD::FCOS , MVT::f64, Expand);
00122   setOperationAction(ISD::FREM , MVT::f64, Expand);
00123   setOperationAction(ISD::FPOW , MVT::f64, Expand);
00124   setOperationAction(ISD::FSIN , MVT::f32, Expand);
00125   setOperationAction(ISD::FCOS , MVT::f32, Expand);
00126   setOperationAction(ISD::FREM , MVT::f32, Expand);
00127   setOperationAction(ISD::FPOW , MVT::f32, Expand);
00128 
00129   setOperationAction(ISD::FLT_ROUNDS_, MVT::i32, Custom);
00130 
00131   // If we're enabling GP optimizations, use hardware square root
00132   if (!TM.getSubtarget<PPCSubtarget>().hasFSQRT()) {
00133     setOperationAction(ISD::FSQRT, MVT::f64, Expand);
00134     setOperationAction(ISD::FSQRT, MVT::f32, Expand);
00135   }
00136 
00137   setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
00138   setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
00139 
00140   // PowerPC does not have BSWAP, CTPOP or CTTZ
00141   setOperationAction(ISD::BSWAP, MVT::i32  , Expand);
00142   setOperationAction(ISD::CTPOP, MVT::i32  , Expand);
00143   setOperationAction(ISD::CTTZ , MVT::i32  , Expand);
00144   setOperationAction(ISD::BSWAP, MVT::i64  , Expand);
00145   setOperationAction(ISD::CTPOP, MVT::i64  , Expand);
00146   setOperationAction(ISD::CTTZ , MVT::i64  , Expand);
00147 
00148   // PowerPC does not have ROTR
00149   setOperationAction(ISD::ROTR, MVT::i32   , Expand);
00150   setOperationAction(ISD::ROTR, MVT::i64   , Expand);
00151 
00152   // PowerPC does not have Select
00153   setOperationAction(ISD::SELECT, MVT::i32, Expand);
00154   setOperationAction(ISD::SELECT, MVT::i64, Expand);
00155   setOperationAction(ISD::SELECT, MVT::f32, Expand);
00156   setOperationAction(ISD::SELECT, MVT::f64, Expand);
00157 
00158   // PowerPC wants to turn select_cc of FP into fsel when possible.
00159   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
00160   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
00161 
00162   // PowerPC wants to optimize integer setcc a bit
00163   setOperationAction(ISD::SETCC, MVT::i32, Custom);
00164 
00165   // PowerPC does not have BRCOND which requires SetCC
00166   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
00167 
00168   setOperationAction(ISD::BR_JT,  MVT::Other, Expand);
00169 
00170   // PowerPC turns FP_TO_SINT into FCTIWZ and some load/stores.
00171   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
00172 
00173   // PowerPC does not have [U|S]INT_TO_FP
00174   setOperationAction(ISD::SINT_TO_FP, MVT::i32, Expand);
00175   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
00176 
00177   setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
00178   setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
00179   setOperationAction(ISD::BIT_CONVERT, MVT::i64, Expand);
00180   setOperationAction(ISD::BIT_CONVERT, MVT::f64, Expand);
00181 
00182   // We cannot sextinreg(i1).  Expand to shifts.
00183   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
00184 
00185   // Support label based line numbers.
00186   setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
00187   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
00188 
00189   setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
00190   setOperationAction(ISD::EHSELECTION,   MVT::i64, Expand);
00191   setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
00192   setOperationAction(ISD::EHSELECTION,   MVT::i32, Expand);
00193 
00194 
00195   // We want to legalize GlobalAddress and ConstantPool nodes into the
00196   // appropriate instructions to materialize the address.
00197   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
00198   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
00199   setOperationAction(ISD::ConstantPool,  MVT::i32, Custom);
00200   setOperationAction(ISD::JumpTable,     MVT::i32, Custom);
00201   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
00202   setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
00203   setOperationAction(ISD::ConstantPool,  MVT::i64, Custom);
00204   setOperationAction(ISD::JumpTable,     MVT::i64, Custom);
00205 
00206   // TRAP is legal.
00207   setOperationAction(ISD::TRAP, MVT::Other, Legal);
00208 
00209   // TRAMPOLINE is custom lowered.
00210   setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
00211 
00212   // VASTART needs to be custom lowered to use the VarArgsFrameIndex
00213   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
00214 
00215   // VAARG is custom lowered with the 32-bit SVR4 ABI.
00216   if (    TM.getSubtarget<PPCSubtarget>().isSVR4ABI()
00217       && !TM.getSubtarget<PPCSubtarget>().isPPC64())
00218     setOperationAction(ISD::VAARG, MVT::Other, Custom);
00219   else
00220     setOperationAction(ISD::VAARG, MVT::Other, Expand);
00221 
00222   // Use the default implementation.
00223   setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
00224   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
00225   setOperationAction(ISD::STACKSAVE         , MVT::Other, Expand);
00226   setOperationAction(ISD::STACKRESTORE      , MVT::Other, Custom);
00227   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Custom);
00228   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64  , Custom);
00229 
00230   // We want to custom lower some of our intrinsics.
00231   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
00232 
00233   // Comparisons that require checking two conditions.
00234   setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
00235   setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
00236   setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
00237   setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
00238   setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
00239   setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
00240   setCondCodeAction(ISD::SETOGE, MVT::f32, Expand);
00241   setCondCodeAction(ISD::SETOGE, MVT::f64, Expand);
00242   setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
00243   setCondCodeAction(ISD::SETOLE, MVT::f64, Expand);
00244   setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
00245   setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
00246 
00247   if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
00248     // They also have instructions for converting between i64 and fp.
00249     setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
00250     setOperationAction(ISD::FP_TO_UINT, MVT::i64, Expand);
00251     setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
00252     setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
00253     // This is just the low 32 bits of a (signed) fp->i64 conversion.
00254     // We cannot do this with Promote because i64 is not a legal type.
00255     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
00256 
00257     // FIXME: disable this lowered code.  This generates 64-bit register values,
00258     // and we don't model the fact that the top part is clobbered by calls.  We
00259     // need to flag these together so that the value isn't live across a call.
00260     //setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
00261   } else {
00262     // PowerPC does not have FP_TO_UINT on 32-bit implementations.
00263     setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
00264   }
00265 
00266   if (TM.getSubtarget<PPCSubtarget>().use64BitRegs()) {
00267     // 64-bit PowerPC implementations can support i64 types directly
00268     addRegisterClass(MVT::i64, PPC::G8RCRegisterClass);
00269     // BUILD_PAIR can't be handled natively, and should be expanded to shl/or
00270     setOperationAction(ISD::BUILD_PAIR, MVT::i64, Expand);
00271     // 64-bit PowerPC wants to expand i128 shifts itself.
00272     setOperationAction(ISD::SHL_PARTS, MVT::i64, Custom);
00273     setOperationAction(ISD::SRA_PARTS, MVT::i64, Custom);
00274     setOperationAction(ISD::SRL_PARTS, MVT::i64, Custom);
00275   } else {
00276     // 32-bit PowerPC wants to expand i64 shifts itself.
00277     setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
00278     setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
00279     setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
00280   }
00281 
00282   if (TM.getSubtarget<PPCSubtarget>().hasAltivec()) {
00283     // First set operation action for all vector types to expand. Then we
00284     // will selectively turn on ones that can be effectively codegen'd.
00285     for (unsigned i = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
00286          i <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++i) {
00287       MVT::SimpleValueType VT = (MVT::SimpleValueType)i;
00288 
00289       // add/sub are legal for all supported vector VT's.
00290       setOperationAction(ISD::ADD , VT, Legal);
00291       setOperationAction(ISD::SUB , VT, Legal);
00292 
00293       // We promote all shuffles to v16i8.
00294       setOperationAction(ISD::VECTOR_SHUFFLE, VT, Promote);
00295       AddPromotedToType (ISD::VECTOR_SHUFFLE, VT, MVT::v16i8);
00296 
00297       // We promote all non-typed operations to v4i32.
00298       setOperationAction(ISD::AND   , VT, Promote);
00299       AddPromotedToType (ISD::AND   , VT, MVT::v4i32);
00300       setOperationAction(ISD::OR    , VT, Promote);
00301       AddPromotedToType (ISD::OR    , VT, MVT::v4i32);
00302       setOperationAction(ISD::XOR   , VT, Promote);
00303       AddPromotedToType (ISD::XOR   , VT, MVT::v4i32);
00304       setOperationAction(ISD::LOAD  , VT, Promote);
00305       AddPromotedToType (ISD::LOAD  , VT, MVT::v4i32);
00306       setOperationAction(ISD::SELECT, VT, Promote);
00307       AddPromotedToType (ISD::SELECT, VT, MVT::v4i32);
00308       setOperationAction(ISD::STORE, VT, Promote);
00309       AddPromotedToType (ISD::STORE, VT, MVT::v4i32);
00310 
00311       // No other operations are legal.
00312       setOperationAction(ISD::MUL , VT, Expand);
00313       setOperationAction(ISD::SDIV, VT, Expand);
00314       setOperationAction(ISD::SREM, VT, Expand);
00315       setOperationAction(ISD::UDIV, VT, Expand);
00316       setOperationAction(ISD::UREM, VT, Expand);
00317       setOperationAction(ISD::FDIV, VT, Expand);
00318       setOperationAction(ISD::FNEG, VT, Expand);
00319       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Expand);
00320       setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
00321       setOperationAction(ISD::BUILD_VECTOR, VT, Expand);
00322       setOperationAction(ISD::UMUL_LOHI, VT, Expand);
00323       setOperationAction(ISD::SMUL_LOHI, VT, Expand);
00324       setOperationAction(ISD::UDIVREM, VT, Expand);
00325       setOperationAction(ISD::SDIVREM, VT, Expand);
00326       setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Expand);
00327       setOperationAction(ISD::FPOW, VT, Expand);
00328       setOperationAction(ISD::CTPOP, VT, Expand);
00329       setOperationAction(ISD::CTLZ, VT, Expand);
00330       setOperationAction(ISD::CTTZ, VT, Expand);
00331     }
00332 
00333     // We can custom expand all VECTOR_SHUFFLEs to VPERM, others we can handle
00334     // with merges, splats, etc.
00335     setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
00336 
00337     setOperationAction(ISD::AND   , MVT::v4i32, Legal);
00338     setOperationAction(ISD::OR    , MVT::v4i32, Legal);
00339     setOperationAction(ISD::XOR   , MVT::v4i32, Legal);
00340     setOperationAction(ISD::LOAD  , MVT::v4i32, Legal);
00341     setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
00342     setOperationAction(ISD::STORE , MVT::v4i32, Legal);
00343 
00344     addRegisterClass(MVT::v4f32, PPC::VRRCRegisterClass);
00345     addRegisterClass(MVT::v4i32, PPC::VRRCRegisterClass);
00346     addRegisterClass(MVT::v8i16, PPC::VRRCRegisterClass);
00347     addRegisterClass(MVT::v16i8, PPC::VRRCRegisterClass);
00348 
00349     setOperationAction(ISD::MUL, MVT::v4f32, Legal);
00350     setOperationAction(ISD::MUL, MVT::v4i32, Custom);
00351     setOperationAction(ISD::MUL, MVT::v8i16, Custom);
00352     setOperationAction(ISD::MUL, MVT::v16i8, Custom);
00353 
00354     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Custom);
00355     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i32, Custom);
00356 
00357     setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
00358     setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
00359     setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
00360     setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
00361   }
00362 
00363   setShiftAmountType(MVT::i32);
00364   setBooleanContents(ZeroOrOneBooleanContent);
00365 
00366   if (TM.getSubtarget<PPCSubtarget>().isPPC64()) {
00367     setStackPointerRegisterToSaveRestore(PPC::X1);
00368     setExceptionPointerRegister(PPC::X3);
00369     setExceptionSelectorRegister(PPC::X4);
00370   } else {
00371     setStackPointerRegisterToSaveRestore(PPC::R1);
00372     setExceptionPointerRegister(PPC::R3);
00373     setExceptionSelectorRegister(PPC::R4);
00374   }
00375 
00376   // We have target-specific dag combine patterns for the following nodes:
00377   setTargetDAGCombine(ISD::SINT_TO_FP);
00378   setTargetDAGCombine(ISD::STORE);
00379   setTargetDAGCombine(ISD::BR_CC);
00380   setTargetDAGCombine(ISD::BSWAP);
00381 
00382   // Darwin long double math library functions have $LDBL128 appended.
00383   if (TM.getSubtarget<PPCSubtarget>().isDarwin()) {
00384     setLibcallName(RTLIB::COS_PPCF128, "cosl$LDBL128");
00385     setLibcallName(RTLIB::POW_PPCF128, "powl$LDBL128");
00386     setLibcallName(RTLIB::REM_PPCF128, "fmodl$LDBL128");
00387     setLibcallName(RTLIB::SIN_PPCF128, "sinl$LDBL128");
00388     setLibcallName(RTLIB::SQRT_PPCF128, "sqrtl$LDBL128");
00389     setLibcallName(RTLIB::LOG_PPCF128, "logl$LDBL128");
00390     setLibcallName(RTLIB::LOG2_PPCF128, "log2l$LDBL128");
00391     setLibcallName(RTLIB::LOG10_PPCF128, "log10l$LDBL128");
00392     setLibcallName(RTLIB::EXP_PPCF128, "expl$LDBL128");
00393     setLibcallName(RTLIB::EXP2_PPCF128, "exp2l$LDBL128");
00394   }
00395 
00396   computeRegisterProperties();
00397 }
00398 
00399 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
00400 /// function arguments in the caller parameter area.
00401 unsigned PPCTargetLowering::getByValTypeAlignment(const Type *Ty) const {
00402   TargetMachine &TM = getTargetMachine();
00403   // Darwin passes everything on 4 byte boundary.
00404   if (TM.getSubtarget<PPCSubtarget>().isDarwin())
00405     return 4;
00406   // FIXME SVR4 TBD
00407   return 4;
00408 }
00409 
00410 const char *PPCTargetLowering::getTargetNodeName(unsigned Opcode) const {
00411   switch (Opcode) {
00412   default: return 0;
00413   case PPCISD::FSEL:            return "PPCISD::FSEL";
00414   case PPCISD::FCFID:           return "PPCISD::FCFID";
00415   case PPCISD::FCTIDZ:          return "PPCISD::FCTIDZ";
00416   case PPCISD::FCTIWZ:          return "PPCISD::FCTIWZ";
00417   case PPCISD::STFIWX:          return "PPCISD::STFIWX";
00418   case PPCISD::VMADDFP:         return "PPCISD::VMADDFP";
00419   case PPCISD::VNMSUBFP:        return "PPCISD::VNMSUBFP";
00420   case PPCISD::VPERM:           return "PPCISD::VPERM";
00421   case PPCISD::Hi:              return "PPCISD::Hi";
00422   case PPCISD::Lo:              return "PPCISD::Lo";
00423   case PPCISD::TOC_ENTRY:       return "PPCISD::TOC_ENTRY";
00424   case PPCISD::DYNALLOC:        return "PPCISD::DYNALLOC";
00425   case PPCISD::GlobalBaseReg:   return "PPCISD::GlobalBaseReg";
00426   case PPCISD::SRL:             return "PPCISD::SRL";
00427   case PPCISD::SRA:             return "PPCISD::SRA";
00428   case PPCISD::SHL:             return "PPCISD::SHL";
00429   case PPCISD::EXTSW_32:        return "PPCISD::EXTSW_32";
00430   case PPCISD::STD_32:          return "PPCISD::STD_32";
00431   case PPCISD::CALL_SVR4:       return "PPCISD::CALL_SVR4";
00432   case PPCISD::CALL_Darwin:     return "PPCISD::CALL_Darwin";
00433   case PPCISD::NOP:             return "PPCISD::NOP";
00434   case PPCISD::MTCTR:           return "PPCISD::MTCTR";
00435   case PPCISD::BCTRL_Darwin:    return "PPCISD::BCTRL_Darwin";
00436   case PPCISD::BCTRL_SVR4:      return "PPCISD::BCTRL_SVR4";
00437   case PPCISD::RET_FLAG:        return "PPCISD::RET_FLAG";
00438   case PPCISD::MFCR:            return "PPCISD::MFCR";
00439   case PPCISD::VCMP:            return "PPCISD::VCMP";
00440   case PPCISD::VCMPo:           return "PPCISD::VCMPo";
00441   case PPCISD::LBRX:            return "PPCISD::LBRX";
00442   case PPCISD::STBRX:           return "PPCISD::STBRX";
00443   case PPCISD::LARX:            return "PPCISD::LARX";
00444   case PPCISD::STCX:            return "PPCISD::STCX";
00445   case PPCISD::COND_BRANCH:     return "PPCISD::COND_BRANCH";
00446   case PPCISD::MFFS:            return "PPCISD::MFFS";
00447   case PPCISD::MTFSB0:          return "PPCISD::MTFSB0";
00448   case PPCISD::MTFSB1:          return "PPCISD::MTFSB1";
00449   case PPCISD::FADDRTZ:         return "PPCISD::FADDRTZ";
00450   case PPCISD::MTFSF:           return "PPCISD::MTFSF";
00451   case PPCISD::TC_RETURN:       return "PPCISD::TC_RETURN";
00452   }
00453 }
00454 
00455 MVT::SimpleValueType PPCTargetLowering::getSetCCResultType(EVT VT) const {
00456   return MVT::i32;
00457 }
00458 
00459 /// getFunctionAlignment - Return the Log2 alignment of this function.
00460 unsigned PPCTargetLowering::getFunctionAlignment(const Function *F) const {
00461   if (getTargetMachine().getSubtarget<PPCSubtarget>().isDarwin())
00462     return F->hasFnAttr(Attribute::OptimizeForSize) ? 2 : 4;
00463   else
00464     return 2;
00465 }
00466 
00467 //===----------------------------------------------------------------------===//
00468 // Node matching predicates, for use by the tblgen matching code.
00469 //===----------------------------------------------------------------------===//
00470 
00471 /// isFloatingPointZero - Return true if this is 0.0 or -0.0.
00472 static bool isFloatingPointZero(SDValue Op) {
00473   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Op))
00474     return CFP->getValueAPF().isZero();
00475   else if (ISD::isEXTLoad(Op.getNode()) || ISD::isNON_EXTLoad(Op.getNode())) {
00476     // Maybe this has already been legalized into the constant pool?
00477     if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op.getOperand(1)))
00478       if (ConstantFP *CFP = dyn_cast<ConstantFP>(CP->getConstVal()))
00479         return CFP->getValueAPF().isZero();
00480   }
00481   return false;
00482 }
00483 
00484 /// isConstantOrUndef - Op is either an undef node or a ConstantSDNode.  Return
00485 /// true if Op is undef or if it matches the specified value.
00486 static bool isConstantOrUndef(int Op, int Val) {
00487   return Op < 0 || Op == Val;
00488 }
00489 
00490 /// isVPKUHUMShuffleMask - Return true if this is the shuffle mask for a
00491 /// VPKUHUM instruction.
00492 bool PPC::isVPKUHUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
00493   if (!isUnary) {
00494     for (unsigned i = 0; i != 16; ++i)
00495       if (!isConstantOrUndef(N->getMaskElt(i),  i*2+1))
00496         return false;
00497   } else {
00498     for (unsigned i = 0; i != 8; ++i)
00499       if (!isConstantOrUndef(N->getMaskElt(i),    i*2+1) ||
00500           !isConstantOrUndef(N->getMaskElt(i+8),  i*2+1))
00501         return false;
00502   }
00503   return true;
00504 }
00505 
00506 /// isVPKUWUMShuffleMask - Return true if this is the shuffle mask for a
00507 /// VPKUWUM instruction.
00508 bool PPC::isVPKUWUMShuffleMask(ShuffleVectorSDNode *N, bool isUnary) {
00509   if (!isUnary) {
00510     for (unsigned i = 0; i != 16; i += 2)
00511       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2+2) ||
00512           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+3))
00513         return false;
00514   } else {
00515     for (unsigned i = 0; i != 8; i += 2)
00516       if (!isConstantOrUndef(N->getMaskElt(i  ),  i*2+2) ||
00517           !isConstantOrUndef(N->getMaskElt(i+1),  i*2+3) ||
00518           !isConstantOrUndef(N->getMaskElt(i+8),  i*2+2) ||
00519           !isConstantOrUndef(N->getMaskElt(i+9),  i*2+3))
00520         return false;
00521   }
00522   return true;
00523 }
00524 
00525 /// isVMerge - Common function, used to match vmrg* shuffles.
00526 ///
00527 static bool isVMerge(ShuffleVectorSDNode *N, unsigned UnitSize,
00528                      unsigned LHSStart, unsigned RHSStart) {
00529   assert(N->getValueType(0) == MVT::v16i8 &&
00530          "PPC only supports shuffles by bytes!");
00531   assert((UnitSize == 1 || UnitSize == 2 || UnitSize == 4) &&
00532          "Unsupported merge size!");
00533 
00534   for (unsigned i = 0; i != 8/UnitSize; ++i)     // Step over units
00535     for (unsigned j = 0; j != UnitSize; ++j) {   // Step over bytes within unit
00536       if (!isConstantOrUndef(N->getMaskElt(i*UnitSize*2+j),
00537                              LHSStart+j+i*UnitSize) ||
00538           !isConstantOrUndef(N->getMaskElt(i*UnitSize*2+UnitSize+j),
00539                              RHSStart+j+i*UnitSize))
00540         return false;
00541     }
00542   return true;
00543 }
00544 
00545 /// isVMRGLShuffleMask - Return true if this is a shuffle mask suitable for
00546 /// a VRGL* instruction with the specified unit size (1,2 or 4 bytes).
00547 bool PPC::isVMRGLShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, 
00548                              bool isUnary) {
00549   if (!isUnary)
00550     return isVMerge(N, UnitSize, 8, 24);
00551   return isVMerge(N, UnitSize, 8, 8);
00552 }
00553 
00554 /// isVMRGHShuffleMask - Return true if this is a shuffle mask suitable for
00555 /// a VRGH* instruction with the specified unit size (1,2 or 4 bytes).
00556 bool PPC::isVMRGHShuffleMask(ShuffleVectorSDNode *N, unsigned UnitSize, 
00557                              bool isUnary) {
00558   if (!isUnary)
00559     return isVMerge(N, UnitSize, 0, 16);
00560   return isVMerge(N, UnitSize, 0, 0);
00561 }
00562 
00563 
00564 /// isVSLDOIShuffleMask - If this is a vsldoi shuffle mask, return the shift
00565 /// amount, otherwise return -1.
00566 int PPC::isVSLDOIShuffleMask(SDNode *N, bool isUnary) {
00567   assert(N->getValueType(0) == MVT::v16i8 &&
00568          "PPC only supports shuffles by bytes!");
00569 
00570   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
00571   
00572   // Find the first non-undef value in the shuffle mask.
00573   unsigned i;
00574   for (i = 0; i != 16 && SVOp->getMaskElt(i) < 0; ++i)
00575     /*search*/;
00576 
00577   if (i == 16) return -1;  // all undef.
00578 
00579   // Otherwise, check to see if the rest of the elements are consecutively
00580   // numbered from this value.
00581   unsigned ShiftAmt = SVOp->getMaskElt(i);
00582   if (ShiftAmt < i) return -1;
00583   ShiftAmt -= i;
00584 
00585   if (!isUnary) {
00586     // Check the rest of the elements to see if they are consecutive.
00587     for (++i; i != 16; ++i)
00588       if (!isConstantOrUndef(SVOp->getMaskElt(i), ShiftAmt+i))
00589         return -1;
00590   } else {
00591     // Check the rest of the elements to see if they are consecutive.
00592     for (++i; i != 16; ++i)
00593       if (!isConstantOrUndef(SVOp->getMaskElt(i), (ShiftAmt+i) & 15))
00594         return -1;
00595   }
00596   return ShiftAmt;
00597 }
00598 
00599 /// isSplatShuffleMask - Return true if the specified VECTOR_SHUFFLE operand
00600 /// specifies a splat of a single element that is suitable for input to
00601 /// VSPLTB/VSPLTH/VSPLTW.
00602 bool PPC::isSplatShuffleMask(ShuffleVectorSDNode *N, unsigned EltSize) {
00603   assert(N->getValueType(0) == MVT::v16i8 &&
00604          (EltSize == 1 || EltSize == 2 || EltSize == 4));
00605 
00606   // This is a splat operation if each element of the permute is the same, and
00607   // if the value doesn't reference the second vector.
00608   unsigned ElementBase = N->getMaskElt(0);
00609   
00610   // FIXME: Handle UNDEF elements too!
00611   if (ElementBase >= 16)
00612     return false;
00613 
00614   // Check that the indices are consecutive, in the case of a multi-byte element
00615   // splatted with a v16i8 mask.
00616   for (unsigned i = 1; i != EltSize; ++i)
00617     if (N->getMaskElt(i) < 0 || N->getMaskElt(i) != (int)(i+ElementBase))
00618       return false;
00619 
00620   for (unsigned i = EltSize, e = 16; i != e; i += EltSize) {
00621     if (N->getMaskElt(i) < 0) continue;
00622     for (unsigned j = 0; j != EltSize; ++j)
00623       if (N->getMaskElt(i+j) != N->getMaskElt(j))
00624         return false;
00625   }
00626   return true;
00627 }
00628 
00629 /// isAllNegativeZeroVector - Returns true if all elements of build_vector
00630 /// are -0.0.
00631 bool PPC::isAllNegativeZeroVector(SDNode *N) {
00632   BuildVectorSDNode *BV = cast<BuildVectorSDNode>(N);
00633 
00634   APInt APVal, APUndef;
00635   unsigned BitSize;
00636   bool HasAnyUndefs;
00637   
00638   if (BV->isConstantSplat(APVal, APUndef, BitSize, HasAnyUndefs, 32))
00639     if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
00640       return CFP->getValueAPF().isNegZero();
00641 
00642   return false;
00643 }
00644 
00645 /// getVSPLTImmediate - Return the appropriate VSPLT* immediate to splat the
00646 /// specified isSplatShuffleMask VECTOR_SHUFFLE mask.
00647 unsigned PPC::getVSPLTImmediate(SDNode *N, unsigned EltSize) {
00648   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
00649   assert(isSplatShuffleMask(SVOp, EltSize));
00650   return SVOp->getMaskElt(0) / EltSize;
00651 }
00652 
00653 /// get_VSPLTI_elt - If this is a build_vector of constants which can be formed
00654 /// by using a vspltis[bhw] instruction of the specified element size, return
00655 /// the constant being splatted.  The ByteSize field indicates the number of
00656 /// bytes of each element [124] -> [bhw].
00657 SDValue PPC::get_VSPLTI_elt(SDNode *N, unsigned ByteSize, SelectionDAG &DAG) {
00658   SDValue OpVal(0, 0);
00659 
00660   // If ByteSize of the splat is bigger than the element size of the
00661   // build_vector, then we have a case where we are checking for a splat where
00662   // multiple elements of the buildvector are folded together into a single
00663   // logical element of the splat (e.g. "vsplish 1" to splat {0,1}*8).
00664   unsigned EltSize = 16/N->getNumOperands();
00665   if (EltSize < ByteSize) {
00666     unsigned Multiple = ByteSize/EltSize;   // Number of BV entries per spltval.
00667     SDValue UniquedVals[4];
00668     assert(Multiple > 1 && Multiple <= 4 && "How can this happen?");
00669 
00670     // See if all of the elements in the buildvector agree across.
00671     for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
00672       if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
00673       // If the element isn't a constant, bail fully out.
00674       if (!isa<ConstantSDNode>(N->getOperand(i))) return SDValue();
00675 
00676 
00677       if (UniquedVals[i&(Multiple-1)].getNode() == 0)
00678         UniquedVals[i&(Multiple-1)] = N->getOperand(i);
00679       else if (UniquedVals[i&(Multiple-1)] != N->getOperand(i))
00680         return SDValue();  // no match.
00681     }
00682 
00683     // Okay, if we reached this point, UniquedVals[0..Multiple-1] contains
00684     // either constant or undef values that are identical for each chunk.  See
00685     // if these chunks can form into a larger vspltis*.
00686 
00687     // Check to see if all of the leading entries are either 0 or -1.  If
00688     // neither, then this won't fit into the immediate field.
00689     bool LeadingZero = true;
00690     bool LeadingOnes = true;
00691     for (unsigned i = 0; i != Multiple-1; ++i) {
00692       if (UniquedVals[i].getNode() == 0) continue;  // Must have been undefs.
00693 
00694       LeadingZero &= cast<ConstantSDNode>(UniquedVals[i])->isNullValue();
00695       LeadingOnes &= cast<ConstantSDNode>(UniquedVals[i])->isAllOnesValue();
00696     }
00697     // Finally, check the least significant entry.
00698     if (LeadingZero) {
00699       if (UniquedVals[Multiple-1].getNode() == 0)
00700         return DAG.getTargetConstant(0, MVT::i32);  // 0,0,0,undef
00701       int Val = cast<ConstantSDNode>(UniquedVals[Multiple-1])->getZExtValue();
00702       if (Val < 16)
00703         return DAG.getTargetConstant(Val, MVT::i32);  // 0,0,0,4 -> vspltisw(4)
00704     }
00705     if (LeadingOnes) {
00706       if (UniquedVals[Multiple-1].getNode() == 0)
00707         return DAG.getTargetConstant(~0U, MVT::i32);  // -1,-1,-1,undef
00708       int Val =cast<ConstantSDNode>(UniquedVals[Multiple-1])->getSExtValue();
00709       if (Val >= -16)                            // -1,-1,-1,-2 -> vspltisw(-2)
00710         return DAG.getTargetConstant(Val, MVT::i32);
00711     }
00712 
00713     return SDValue();
00714   }
00715 
00716   // Check to see if this buildvec has a single non-undef value in its elements.
00717   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
00718     if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
00719     if (OpVal.getNode() == 0)
00720       OpVal = N->getOperand(i);
00721     else if (OpVal != N->getOperand(i))
00722       return SDValue();
00723   }
00724 
00725   if (OpVal.getNode() == 0) return SDValue();  // All UNDEF: use implicit def.
00726 
00727   unsigned ValSizeInBytes = EltSize;
00728   uint64_t Value = 0;
00729   if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(OpVal)) {
00730     Value = CN->getZExtValue();
00731   } else if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(OpVal)) {
00732     assert(CN->getValueType(0) == MVT::f32 && "Only one legal FP vector type!");
00733     Value = FloatToBits(CN->getValueAPF().convertToFloat());
00734   }
00735 
00736   // If the splat value is larger than the element value, then we can never do
00737   // this splat.  The only case that we could fit the replicated bits into our
00738   // immediate field for would be zero, and we prefer to use vxor for it.
00739   if (ValSizeInBytes < ByteSize) return SDValue();
00740 
00741   // If the element value is larger than the splat value, cut it in half and
00742   // check to see if the two halves are equal.  Continue doing this until we
00743   // get to ByteSize.  This allows us to handle 0x01010101 as 0x01.
00744   while (ValSizeInBytes > ByteSize) {
00745     ValSizeInBytes >>= 1;
00746 
00747     // If the top half equals the bottom half, we're still ok.
00748     if (((Value >> (ValSizeInBytes*8)) & ((1 << (8*ValSizeInBytes))-1)) !=
00749          (Value                        & ((1 << (8*ValSizeInBytes))-1)))
00750       return SDValue();
00751   }
00752 
00753   // Properly sign extend the value.
00754   int ShAmt = (4-ByteSize)*8;
00755   int MaskVal = ((int)Value << ShAmt) >> ShAmt;
00756 
00757   // If this is zero, don't match, zero matches ISD::isBuildVectorAllZeros.
00758   if (MaskVal == 0) return SDValue();
00759 
00760   // Finally, if this value fits in a 5 bit sext field, return it
00761   if (((MaskVal << (32-5)) >> (32-5)) == MaskVal)
00762     return DAG.getTargetConstant(MaskVal, MVT::i32);
00763   return SDValue();
00764 }
00765 
00766 //===----------------------------------------------------------------------===//
00767 //  Addressing Mode Selection
00768 //===----------------------------------------------------------------------===//
00769 
00770 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
00771 /// or 64-bit immediate, and if the value can be accurately represented as a
00772 /// sign extension from a 16-bit value.  If so, this returns true and the
00773 /// immediate.
00774 static bool isIntS16Immediate(SDNode *N, short &Imm) {
00775   if (N->getOpcode() != ISD::Constant)
00776     return false;
00777 
00778   Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
00779   if (N->getValueType(0) == MVT::i32)
00780     return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
00781   else
00782     return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
00783 }
00784 static bool isIntS16Immediate(SDValue Op, short &Imm) {
00785   return isIntS16Immediate(Op.getNode(), Imm);
00786 }
00787 
00788 
00789 /// SelectAddressRegReg - Given the specified addressed, check to see if it
00790 /// can be represented as an indexed [r+r] operation.  Returns false if it
00791 /// can be more efficiently represented with [r+imm].
00792 bool PPCTargetLowering::SelectAddressRegReg(SDValue N, SDValue &Base,
00793                                             SDValue &Index,
00794                                             SelectionDAG &DAG) const {
00795   short imm = 0;
00796   if (N.getOpcode() == ISD::ADD) {
00797     if (isIntS16Immediate(N.getOperand(1), imm))
00798       return false;    // r+i
00799     if (N.getOperand(1).getOpcode() == PPCISD::Lo)
00800       return false;    // r+i
00801 
00802     Base = N.getOperand(0);
00803     Index = N.getOperand(1);
00804     return true;
00805   } else if (N.getOpcode() == ISD::OR) {
00806     if (isIntS16Immediate(N.getOperand(1), imm))
00807       return false;    // r+i can fold it if we can.
00808 
00809     // If this is an or of disjoint bitfields, we can codegen this as an add
00810     // (for better address arithmetic) if the LHS and RHS of the OR are provably
00811     // disjoint.
00812     APInt LHSKnownZero, LHSKnownOne;
00813     APInt RHSKnownZero, RHSKnownOne;
00814     DAG.ComputeMaskedBits(N.getOperand(0),
00815                           APInt::getAllOnesValue(N.getOperand(0)
00816                             .getValueSizeInBits()),
00817                           LHSKnownZero, LHSKnownOne);
00818 
00819     if (LHSKnownZero.getBoolValue()) {
00820       DAG.ComputeMaskedBits(N.getOperand(1),
00821                             APInt::getAllOnesValue(N.getOperand(1)
00822                               .getValueSizeInBits()),
00823                             RHSKnownZero, RHSKnownOne);
00824       // If all of the bits are known zero on the LHS or RHS, the add won't
00825       // carry.
00826       if (~(LHSKnownZero | RHSKnownZero) == 0) {
00827         Base = N.getOperand(0);
00828         Index = N.getOperand(1);
00829         return true;
00830       }
00831     }
00832   }
00833 
00834   return false;
00835 }
00836 
00837 /// Returns true if the address N can be represented by a base register plus
00838 /// a signed 16-bit displacement [r+imm], and if it is not better
00839 /// represented as reg+reg.
00840 bool PPCTargetLowering::SelectAddressRegImm(SDValue N, SDValue &Disp,
00841                                             SDValue &Base,
00842                                             SelectionDAG &DAG) const {
00843   // FIXME dl should come from parent load or store, not from address
00844   DebugLoc dl = N.getDebugLoc();
00845   // If this can be more profitably realized as r+r, fail.
00846   if (SelectAddressRegReg(N, Disp, Base, DAG))
00847     return false;
00848 
00849   if (N.getOpcode() == ISD::ADD) {
00850     short imm = 0;
00851     if (isIntS16Immediate(N.getOperand(1), imm)) {
00852       Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
00853       if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
00854         Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
00855       } else {
00856         Base = N.getOperand(0);
00857       }
00858       return true; // [r+i]
00859     } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
00860       // Match LOAD (ADD (X, Lo(G))).
00861      assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
00862              && "Cannot handle constant offsets yet!");
00863       Disp = N.getOperand(1).getOperand(0);  // The global address.
00864       assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
00865              Disp.getOpcode() == ISD::TargetConstantPool ||
00866              Disp.getOpcode() == ISD::TargetJumpTable);
00867       Base = N.getOperand(0);
00868       return true;  // [&g+r]
00869     }
00870   } else if (N.getOpcode() == ISD::OR) {
00871     short imm = 0;
00872     if (isIntS16Immediate(N.getOperand(1), imm)) {
00873       // If this is an or of disjoint bitfields, we can codegen this as an add
00874       // (for better address arithmetic) if the LHS and RHS of the OR are
00875       // provably disjoint.
00876       APInt LHSKnownZero, LHSKnownOne;
00877       DAG.ComputeMaskedBits(N.getOperand(0),
00878                             APInt::getAllOnesValue(N.getOperand(0)
00879                                                    .getValueSizeInBits()),
00880                             LHSKnownZero, LHSKnownOne);
00881 
00882       if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
00883         // If all of the bits are known zero on the LHS or RHS, the add won't
00884         // carry.
00885         Base = N.getOperand(0);
00886         Disp = DAG.getTargetConstant((int)imm & 0xFFFF, MVT::i32);
00887         return true;
00888       }
00889     }
00890   } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
00891     // Loading from a constant address.
00892 
00893     // If this address fits entirely in a 16-bit sext immediate field, codegen
00894     // this as "d, 0"
00895     short Imm;
00896     if (isIntS16Immediate(CN, Imm)) {
00897       Disp = DAG.getTargetConstant(Imm, CN->getValueType(0));
00898       Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
00899       return true;
00900     }
00901 
00902     // Handle 32-bit sext immediates with LIS + addr mode.
00903     if (CN->getValueType(0) == MVT::i32 ||
00904         (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
00905       int Addr = (int)CN->getZExtValue();
00906 
00907       // Otherwise, break this down into an LIS + disp.
00908       Disp = DAG.getTargetConstant((short)Addr, MVT::i32);
00909 
00910       Base = DAG.getTargetConstant((Addr - (signed short)Addr) >> 16, MVT::i32);
00911       unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
00912       Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base), 0);
00913       return true;
00914     }
00915   }
00916 
00917   Disp = DAG.getTargetConstant(0, getPointerTy());
00918   if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
00919     Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
00920   else
00921     Base = N;
00922   return true;      // [r+0]
00923 }
00924 
00925 /// SelectAddressRegRegOnly - Given the specified addressed, force it to be
00926 /// represented as an indexed [r+r] operation.
00927 bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
00928                                                 SDValue &Index,
00929                                                 SelectionDAG &DAG) const {
00930   // Check to see if we can easily represent this as an [r+r] address.  This
00931   // will fail if it thinks that the address is more profitably represented as
00932   // reg+imm, e.g. where imm = 0.
00933   if (SelectAddressRegReg(N, Base, Index, DAG))
00934     return true;
00935 
00936   // If the operand is an addition, always emit this as [r+r], since this is
00937   // better (for code size, and execution, as the memop does the add for free)
00938   // than emitting an explicit add.
00939   if (N.getOpcode() == ISD::ADD) {
00940     Base = N.getOperand(0);
00941     Index = N.getOperand(1);
00942     return true;
00943   }
00944 
00945   // Otherwise, do it the hard way, using R0 as the base register.
00946   Base = DAG.getRegister(PPC::R0, N.getValueType());
00947   Index = N;
00948   return true;
00949 }
00950 
00951 /// SelectAddressRegImmShift - Returns true if the address N can be
00952 /// represented by a base register plus a signed 14-bit displacement
00953 /// [r+imm*4].  Suitable for use by STD and friends.
00954 bool PPCTargetLowering::SelectAddressRegImmShift(SDValue N, SDValue &Disp,
00955                                                  SDValue &Base,
00956                                                  SelectionDAG &DAG) const {
00957   // FIXME dl should come from the parent load or store, not the address
00958   DebugLoc dl = N.getDebugLoc();
00959   // If this can be more profitably realized as r+r, fail.
00960   if (SelectAddressRegReg(N, Disp, Base, DAG))
00961     return false;
00962 
00963   if (N.getOpcode() == ISD::ADD) {
00964     short imm = 0;
00965     if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
00966       Disp =  DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
00967       if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
00968         Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
00969       } else {
00970         Base = N.getOperand(0);
00971       }
00972       return true; // [r+i]
00973     } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
00974       // Match LOAD (ADD (X, Lo(G))).
00975      assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getZExtValue()
00976              && "Cannot handle constant offsets yet!");
00977       Disp = N.getOperand(1).getOperand(0);  // The global address.
00978       assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
00979              Disp.getOpcode() == ISD::TargetConstantPool ||
00980              Disp.getOpcode() == ISD::TargetJumpTable);
00981       Base = N.getOperand(0);
00982       return true;  // [&g+r]
00983     }
00984   } else if (N.getOpcode() == ISD::OR) {
00985     short imm = 0;
00986     if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
00987       // If this is an or of disjoint bitfields, we can codegen this as an add
00988       // (for better address arithmetic) if the LHS and RHS of the OR are
00989       // provably disjoint.
00990       APInt LHSKnownZero, LHSKnownOne;
00991       DAG.ComputeMaskedBits(N.getOperand(0),
00992                             APInt::getAllOnesValue(N.getOperand(0)
00993                                                    .getValueSizeInBits()),
00994                             LHSKnownZero, LHSKnownOne);
00995       if ((LHSKnownZero.getZExtValue()|~(uint64_t)imm) == ~0ULL) {
00996         // If all of the bits are known zero on the LHS or RHS, the add won't
00997         // carry.
00998         Base = N.getOperand(0);
00999         Disp = DAG.getTargetConstant(((int)imm & 0xFFFF) >> 2, MVT::i32);
01000         return true;
01001       }
01002     }
01003   } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
01004     // Loading from a constant address.  Verify low two bits are clear.
01005     if ((CN->getZExtValue() & 3) == 0) {
01006       // If this address fits entirely in a 14-bit sext immediate field, codegen
01007       // this as "d, 0"
01008       short Imm;
01009       if (isIntS16Immediate(CN, Imm)) {
01010         Disp = DAG.getTargetConstant((unsigned short)Imm >> 2, getPointerTy());
01011         Base = DAG.getRegister(PPC::R0, CN->getValueType(0));
01012         return true;
01013       }
01014 
01015       // Fold the low-part of 32-bit absolute addresses into addr mode.
01016       if (CN->getValueType(0) == MVT::i32 ||
01017           (int64_t)CN->getZExtValue() == (int)CN->getZExtValue()) {
01018         int Addr = (int)CN->getZExtValue();
01019 
01020         // Otherwise, break this down into an LIS + disp.
01021         Disp = DAG.getTargetConstant((short)Addr >> 2, MVT::i32);
01022         Base = DAG.getTargetConstant((Addr-(signed short)Addr) >> 16, MVT::i32);
01023         unsigned Opc = CN->getValueType(0) == MVT::i32 ? PPC::LIS : PPC::LIS8;
01024         Base = SDValue(DAG.getTargetNode(Opc, dl, CN->getValueType(0), Base),0);
01025         return true;
01026       }
01027     }
01028   }
01029 
01030   Disp = DAG.getTargetConstant(0, getPointerTy());
01031   if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
01032     Base = DAG.getTargetFrameIndex(FI->getIndex(), N.getValueType());
01033   else
01034     Base = N;
01035   return true;      // [r+0]
01036 }
01037 
01038 
01039 /// getPreIndexedAddressParts - returns true by value, base pointer and
01040 /// offset pointer and addressing mode by reference if the node's address
01041 /// can be legally represented as pre-indexed load / store address.
01042 bool PPCTargetLowering::getPreIndexedAddressParts(SDNode *N, SDValue &Base,
01043                                                   SDValue &Offset,
01044                                                   ISD::MemIndexedMode &AM,
01045                                                   SelectionDAG &DAG) const {
01046   // Disabled by default for now.
01047   if (!EnablePPCPreinc) return false;
01048 
01049   SDValue Ptr;
01050   EVT VT;
01051   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
01052     Ptr = LD->getBasePtr();
01053     VT = LD->getMemoryVT();
01054 
01055   } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
01056     ST = ST;
01057     Ptr = ST->getBasePtr();
01058     VT  = ST->getMemoryVT();
01059   } else
01060     return false;
01061 
01062   // PowerPC doesn't have preinc load/store instructions for vectors.
01063   if (VT.isVector())
01064     return false;
01065 
01066   // TODO: Check reg+reg first.
01067 
01068   // LDU/STU use reg+imm*4, others use reg+imm.
01069   if (VT != MVT::i64) {
01070     // reg + imm
01071     if (!SelectAddressRegImm(Ptr, Offset, Base, DAG))
01072       return false;
01073   } else {
01074     // reg + imm * 4.
01075     if (!SelectAddressRegImmShift(Ptr, Offset, Base, DAG))
01076       return false;
01077   }
01078 
01079   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
01080     // PPC64 doesn't have lwau, but it does have lwaux.  Reject preinc load of
01081     // sext i32 to i64 when addr mode is r+i.
01082     if (LD->getValueType(0) == MVT::i64 && LD->getMemoryVT() == MVT::i32 &&
01083         LD->getExtensionType() == ISD::SEXTLOAD &&
01084         isa<ConstantSDNode>(Offset))
01085       return false;
01086   }
01087 
01088   AM = ISD::PRE_INC;
01089   return true;
01090 }
01091 
01092 //===----------------------------------------------------------------------===//
01093 //  LowerOperation implementation
01094 //===----------------------------------------------------------------------===//
01095 
01096 SDValue PPCTargetLowering::LowerConstantPool(SDValue Op,
01097                                              SelectionDAG &DAG) {
01098   EVT PtrVT = Op.getValueType();
01099   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
01100   Constant *C = CP->getConstVal();
01101   SDValue CPI = DAG.getTargetConstantPool(C, PtrVT, CP->getAlignment());
01102   SDValue Zero = DAG.getConstant(0, PtrVT);
01103   // FIXME there isn't really any debug info here
01104   DebugLoc dl = Op.getDebugLoc();
01105 
01106   const TargetMachine &TM = DAG.getTarget();
01107 
01108   SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, CPI, Zero);
01109   SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, CPI, Zero);
01110 
01111   // If this is a non-darwin platform, we don't support non-static relo models
01112   // yet.
01113   if (TM.getRelocationModel() == Reloc::Static ||
01114       !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
01115     // Generate non-pic code that has direct accesses to the constant pool.
01116     // The address of the global is just (hi(&g)+lo(&g)).
01117     return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
01118   }
01119 
01120   if (TM.getRelocationModel() == Reloc::PIC_) {
01121     // With PIC, the first instruction is actually "GR+hi(&G)".
01122     Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
01123                      DAG.getNode(PPCISD::GlobalBaseReg,
01124                                  DebugLoc::getUnknownLoc(), PtrVT), Hi);
01125   }
01126 
01127   Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
01128   return Lo;
01129 }
01130 
01131 SDValue PPCTargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
01132   EVT PtrVT = Op.getValueType();
01133   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
01134   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
01135   SDValue Zero = DAG.getConstant(0, PtrVT);
01136   // FIXME there isn't really any debug loc here
01137   DebugLoc dl = Op.getDebugLoc();
01138 
01139   const TargetMachine &TM = DAG.getTarget();
01140 
01141   SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, JTI, Zero);
01142   SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, JTI, Zero);
01143 
01144   // If this is a non-darwin platform, we don't support non-static relo models
01145   // yet.
01146   if (TM.getRelocationModel() == Reloc::Static ||
01147       !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
01148     // Generate non-pic code that has direct accesses to the constant pool.
01149     // The address of the global is just (hi(&g)+lo(&g)).
01150     return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
01151   }
01152 
01153   if (TM.getRelocationModel() == Reloc::PIC_) {
01154     // With PIC, the first instruction is actually "GR+hi(&G)".
01155     Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
01156                      DAG.getNode(PPCISD::GlobalBaseReg,
01157                                  DebugLoc::getUnknownLoc(), PtrVT), Hi);
01158   }
01159 
01160   Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
01161   return Lo;
01162 }
01163 
01164 SDValue PPCTargetLowering::LowerGlobalTLSAddress(SDValue Op,
01165                                                    SelectionDAG &DAG) {
01166   llvm_unreachable("TLS not implemented for PPC.");
01167   return SDValue(); // Not reached
01168 }
01169 
01170 SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
01171                                               SelectionDAG &DAG) {
01172   EVT PtrVT = Op.getValueType();
01173   GlobalAddressSDNode *GSDN = cast<GlobalAddressSDNode>(Op);
01174   GlobalValue *GV = GSDN->getGlobal();
01175   SDValue GA = DAG.getTargetGlobalAddress(GV, PtrVT, GSDN->getOffset());
01176   SDValue Zero = DAG.getConstant(0, PtrVT);
01177   // FIXME there isn't really any debug info here
01178   DebugLoc dl = GSDN->getDebugLoc();
01179 
01180   const TargetMachine &TM = DAG.getTarget();
01181 
01182   // 64-bit SVR4 ABI code is always position-independent.
01183   // The actual address of the GlobalValue is stored in the TOC.
01184   if (PPCSubTarget.isSVR4ABI() && PPCSubTarget.isPPC64()) {
01185     return DAG.getNode(PPCISD::TOC_ENTRY, dl, MVT::i64, GA,
01186                        DAG.getRegister(PPC::X2, MVT::i64));
01187   }
01188 
01189   SDValue Hi = DAG.getNode(PPCISD::Hi, dl, PtrVT, GA, Zero);
01190   SDValue Lo = DAG.getNode(PPCISD::Lo, dl, PtrVT, GA, Zero);
01191 
01192   // If this is a non-darwin platform, we don't support non-static relo models
01193   // yet.
01194   if (TM.getRelocationModel() == Reloc::Static ||
01195       !TM.getSubtarget<PPCSubtarget>().isDarwin()) {
01196     // Generate non-pic code that has direct accesses to globals.
01197     // The address of the global is just (hi(&g)+lo(&g)).
01198     return DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
01199   }
01200 
01201   if (TM.getRelocationModel() == Reloc::PIC_) {
01202     // With PIC, the first instruction is actually "GR+hi(&G)".
01203     Hi = DAG.getNode(ISD::ADD, dl, PtrVT,
01204                      DAG.getNode(PPCISD::GlobalBaseReg,
01205                                  DebugLoc::getUnknownLoc(), PtrVT), Hi);
01206   }
01207 
01208   Lo = DAG.getNode(ISD::ADD, dl, PtrVT, Hi, Lo);
01209 
01210   if (!TM.getSubtarget<PPCSubtarget>().hasLazyResolverStub(GV, TM))
01211     return Lo;
01212 
01213   // If the global is weak or external, we have to go through the lazy
01214   // resolution stub.
01215   return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Lo, NULL, 0);
01216 }
01217 
01218 SDValue PPCTargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
01219   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
01220   DebugLoc dl = Op.getDebugLoc();
01221 
01222   // If we're comparing for equality to zero, expose the fact that this is
01223   // implented as a ctlz/srl pair on ppc, so that the dag combiner can
01224   // fold the new nodes.
01225   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
01226     if (C->isNullValue() && CC == ISD::SETEQ) {
01227       EVT VT = Op.getOperand(0).getValueType();
01228       SDValue Zext = Op.getOperand(0);
01229       if (VT.bitsLT(MVT::i32)) {
01230         VT = MVT::i32;
01231         Zext = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Op.getOperand(0));
01232       }
01233       unsigned Log2b = Log2_32(VT.getSizeInBits());
01234       SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Zext);
01235       SDValue Scc = DAG.getNode(ISD::SRL, dl, VT, Clz,
01236                                 DAG.getConstant(Log2b, MVT::i32));
01237       return DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Scc);
01238     }
01239     // Leave comparisons against 0 and -1 alone for now, since they're usually
01240     // optimized.  FIXME: revisit this when we can custom lower all setcc
01241     // optimizations.
01242     if (C->isAllOnesValue() || C->isNullValue())
01243       return SDValue();
01244   }
01245 
01246   // If we have an integer seteq/setne, turn it into a compare against zero
01247   // by xor'ing the rhs with the lhs, which is faster than setting a
01248   // condition register, reading it back out, and masking the correct bit.  The
01249   // normal approach here uses sub to do this instead of xor.  Using xor exposes
01250   // the result to other bit-twiddling opportunities.
01251   EVT LHSVT = Op.getOperand(0).getValueType();
01252   if (LHSVT.isInteger() && (CC == ISD::SETEQ || CC == ISD::SETNE)) {
01253     EVT VT = Op.getValueType();
01254     SDValue Sub = DAG.getNode(ISD::XOR, dl, LHSVT, Op.getOperand(0),
01255                                 Op.getOperand(1));
01256     return DAG.getSetCC(dl, VT, Sub, DAG.getConstant(0, LHSVT), CC);
01257   }
01258   return SDValue();
01259 }
01260 
01261 SDValue PPCTargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG,
01262                               int VarArgsFrameIndex,
01263                               int VarArgsStackOffset,
01264                               unsigned VarArgsNumGPR,
01265                               unsigned VarArgsNumFPR,
01266                               const PPCSubtarget &Subtarget) {
01267 
01268   llvm_unreachable("VAARG not yet implemented for the SVR4 ABI!");
01269   return SDValue(); // Not reached
01270 }
01271 
01272 SDValue PPCTargetLowering::LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
01273   SDValue Chain = Op.getOperand(0);
01274   SDValue Trmp = Op.getOperand(1); // trampoline
01275   SDValue FPtr = Op.getOperand(2); // nested function
01276   SDValue Nest = Op.getOperand(3); // 'nest' parameter value
01277   DebugLoc dl = Op.getDebugLoc();
01278 
01279   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01280   bool isPPC64 = (PtrVT == MVT::i64);
01281   const Type *IntPtrTy =
01282     DAG.getTargetLoweringInfo().getTargetData()->getIntPtrType(
01283                                                              *DAG.getContext());
01284 
01285   TargetLowering::ArgListTy Args;
01286   TargetLowering::ArgListEntry Entry;
01287 
01288   Entry.Ty = IntPtrTy;
01289   Entry.Node = Trmp; Args.push_back(Entry);
01290 
01291   // TrampSize == (isPPC64 ? 48 : 40);
01292   Entry.Node = DAG.getConstant(isPPC64 ? 48 : 40,
01293                                isPPC64 ? MVT::i64 : MVT::i32);
01294   Args.push_back(Entry);
01295 
01296   Entry.Node = FPtr; Args.push_back(Entry);
01297   Entry.Node = Nest; Args.push_back(Entry);
01298 
01299   // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
01300   std::pair<SDValue, SDValue> CallResult =
01301     LowerCallTo(Chain, Op.getValueType().getTypeForEVT(*DAG.getContext()),
01302                 false, false, false, false, 0, CallingConv::C, false,
01303                 /*isReturnValueUsed=*/true,
01304                 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
01305                 Args, DAG, dl);
01306 
01307   SDValue Ops[] =
01308     { CallResult.first, CallResult.second };
01309 
01310   return DAG.getMergeValues(Ops, 2, dl);
01311 }
01312 
01313 SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
01314                                         int VarArgsFrameIndex,
01315                                         int VarArgsStackOffset,
01316                                         unsigned VarArgsNumGPR,
01317                                         unsigned VarArgsNumFPR,
01318                                         const PPCSubtarget &Subtarget) {
01319   DebugLoc dl = Op.getDebugLoc();
01320 
01321   if (Subtarget.isDarwinABI() || Subtarget.isPPC64()) {
01322     // vastart just stores the address of the VarArgsFrameIndex slot into the
01323     // memory location argument.
01324     EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01325     SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
01326     const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
01327     return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
01328   }
01329 
01330   // For the 32-bit SVR4 ABI we follow the layout of the va_list struct.
01331   // We suppose the given va_list is already allocated.
01332   //
01333   // typedef struct {
01334   //  char gpr;     /* index into the array of 8 GPRs
01335   //                 * stored in the register save area
01336   //                 * gpr=0 corresponds to r3,
01337   //                 * gpr=1 to r4, etc.
01338   //                 */
01339   //  char fpr;     /* index into the array of 8 FPRs
01340   //                 * stored in the register save area
01341   //                 * fpr=0 corresponds to f1,
01342   //                 * fpr=1 to f2, etc.
01343   //                 */
01344   //  char *overflow_arg_area;
01345   //                /* location on stack that holds
01346   //                 * the next overflow argument
01347   //                 */
01348   //  char *reg_save_area;
01349   //               /* where r3:r10 and f1:f8 (if saved)
01350   //                * are stored
01351   //                */
01352   // } va_list[1];
01353 
01354 
01355   SDValue ArgGPR = DAG.getConstant(VarArgsNumGPR, MVT::i32);
01356   SDValue ArgFPR = DAG.getConstant(VarArgsNumFPR, MVT::i32);
01357 
01358 
01359   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01360 
01361   SDValue StackOffsetFI = DAG.getFrameIndex(VarArgsStackOffset, PtrVT);
01362   SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
01363 
01364   uint64_t FrameOffset = PtrVT.getSizeInBits()/8;
01365   SDValue ConstFrameOffset = DAG.getConstant(FrameOffset, PtrVT);
01366 
01367   uint64_t StackOffset = PtrVT.getSizeInBits()/8 - 1;
01368   SDValue ConstStackOffset = DAG.getConstant(StackOffset, PtrVT);
01369 
01370   uint64_t FPROffset = 1;
01371   SDValue ConstFPROffset = DAG.getConstant(FPROffset, PtrVT);
01372 
01373   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
01374 
01375   // Store first byte : number of int regs
01376   SDValue firstStore = DAG.getTruncStore(Op.getOperand(0), dl, ArgGPR,
01377                                          Op.getOperand(1), SV, 0, MVT::i8);
01378   uint64_t nextOffset = FPROffset;
01379   SDValue nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, Op.getOperand(1),
01380                                   ConstFPROffset);
01381 
01382   // Store second byte : number of float regs
01383   SDValue secondStore =
01384     DAG.getTruncStore(firstStore, dl, ArgFPR, nextPtr, SV, nextOffset, MVT::i8);
01385   nextOffset += StackOffset;
01386   nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstStackOffset);
01387 
01388   // Store second word : arguments given on stack
01389   SDValue thirdStore =
01390     DAG.getStore(secondStore, dl, StackOffsetFI, nextPtr, SV, nextOffset);
01391   nextOffset += FrameOffset;
01392   nextPtr = DAG.getNode(ISD::ADD, dl, PtrVT, nextPtr, ConstFrameOffset);
01393 
01394   // Store third word : arguments given in registers
01395   return DAG.getStore(thirdStore, dl, FR, nextPtr, SV, nextOffset);
01396 
01397 }
01398 
01399 #include "PPCGenCallingConv.inc"
01400 
01401 static bool CC_PPC_SVR4_Custom_Dummy(unsigned &ValNo, EVT &ValVT, EVT &LocVT,
01402                                      CCValAssign::LocInfo &LocInfo,
01403                                      ISD::ArgFlagsTy &ArgFlags,
01404                                      CCState &State) {
01405   return true;
01406 }
01407 
01408 static bool CC_PPC_SVR4_Custom_AlignArgRegs(unsigned &ValNo, EVT &ValVT,
01409                                             EVT &LocVT,
01410                                             CCValAssign::LocInfo &LocInfo,
01411                                             ISD::ArgFlagsTy &ArgFlags,
01412                                             CCState &State) {
01413   static const unsigned ArgRegs[] = {
01414     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
01415     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
01416   };
01417   const unsigned NumArgRegs = array_lengthof(ArgRegs);
01418   
01419   unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
01420 
01421   // Skip one register if the first unallocated register has an even register
01422   // number and there are still argument registers available which have not been
01423   // allocated yet. RegNum is actually an index into ArgRegs, which means we
01424   // need to skip a register if RegNum is odd.
01425   if (RegNum != NumArgRegs && RegNum % 2 == 1) {
01426     State.AllocateReg(ArgRegs[RegNum]);
01427   }
01428   
01429   // Always return false here, as this function only makes sure that the first
01430   // unallocated register has an odd register number and does not actually
01431   // allocate a register for the current argument.
01432   return false;
01433 }
01434 
01435 static bool CC_PPC_SVR4_Custom_AlignFPArgRegs(unsigned &ValNo, EVT &ValVT,
01436                                               EVT &LocVT,
01437                                               CCValAssign::LocInfo &LocInfo,
01438                                               ISD::ArgFlagsTy &ArgFlags,
01439                                               CCState &State) {
01440   static const unsigned ArgRegs[] = {
01441     PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
01442     PPC::F8
01443   };
01444 
01445   const unsigned NumArgRegs = array_lengthof(ArgRegs);
01446   
01447   unsigned RegNum = State.getFirstUnallocated(ArgRegs, NumArgRegs);
01448 
01449   // If there is only one Floating-point register left we need to put both f64
01450   // values of a split ppc_fp128 value on the stack.
01451   if (RegNum != NumArgRegs && ArgRegs[RegNum] == PPC::F8) {
01452     State.AllocateReg(ArgRegs[RegNum]);
01453   }
01454   
01455   // Always return false here, as this function only makes sure that the two f64
01456   // values a ppc_fp128 value is split into are both passed in registers or both
01457   // passed on the stack and does not actually allocate a register for the
01458   // current argument.
01459   return false;
01460 }
01461 
01462 /// GetFPR - Get the set of FP registers that should be allocated for arguments,
01463 /// on Darwin.
01464 static const unsigned *GetFPR() {
01465   static const unsigned FPR[] = {
01466     PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
01467     PPC::F8, PPC::F9, PPC::F10, PPC::F11, PPC::F12, PPC::F13
01468   };
01469 
01470   return FPR;
01471 }
01472 
01473 /// CalculateStackSlotSize - Calculates the size reserved for this argument on
01474 /// the stack.
01475 static unsigned CalculateStackSlotSize(EVT ArgVT, ISD::ArgFlagsTy Flags,
01476                                        unsigned PtrByteSize) {
01477   unsigned ArgSize = ArgVT.getSizeInBits()/8;
01478   if (Flags.isByVal())
01479     ArgSize = Flags.getByValSize();
01480   ArgSize = ((ArgSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
01481 
01482   return ArgSize;
01483 }
01484 
01485 SDValue
01486 PPCTargetLowering::LowerFormalArguments(SDValue Chain,
01487                                         unsigned CallConv, bool isVarArg,
01488                                         const SmallVectorImpl<ISD::InputArg>
01489                                           &Ins,
01490                                         DebugLoc dl, SelectionDAG &DAG,
01491                                         SmallVectorImpl<SDValue> &InVals) {
01492   if (PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64()) {
01493     return LowerFormalArguments_SVR4(Chain, CallConv, isVarArg, Ins,
01494                                      dl, DAG, InVals);
01495   } else {
01496     return LowerFormalArguments_Darwin(Chain, CallConv, isVarArg, Ins,
01497                                        dl, DAG, InVals);
01498   }
01499 }
01500 
01501 SDValue
01502 PPCTargetLowering::LowerFormalArguments_SVR4(
01503                                       SDValue Chain,
01504                                       unsigned CallConv, bool isVarArg,
01505                                       const SmallVectorImpl<ISD::InputArg>
01506                                         &Ins,
01507                                       DebugLoc dl, SelectionDAG &DAG,
01508                                       SmallVectorImpl<SDValue> &InVals) {
01509 
01510   // 32-bit SVR4 ABI Stack Frame Layout:
01511   //              +-----------------------------------+
01512   //        +-->  |            Back chain             |
01513   //        |     +-----------------------------------+
01514   //        |     | Floating-point register save area |
01515   //        |     +-----------------------------------+
01516   //        |     |    General register save area     |
01517   //        |     +-----------------------------------+
01518   //        |     |          CR save word             |
01519   //        |     +-----------------------------------+
01520   //        |     |         VRSAVE save word          |
01521   //        |     +-----------------------------------+
01522   //        |     |         Alignment padding         |
01523   //        |     +-----------------------------------+
01524   //        |     |     Vector register save area     |
01525   //        |     +-----------------------------------+
01526   //        |     |       Local variable space        |
01527   //        |     +-----------------------------------+
01528   //        |     |        Parameter list area        |
01529   //        |     +-----------------------------------+
01530   //        |     |           LR save word            |
01531   //        |     +-----------------------------------+
01532   // SP-->  +---  |            Back chain             |
01533   //              +-----------------------------------+
01534   //
01535   // Specifications:
01536   //   System V Application Binary Interface PowerPC Processor Supplement
01537   //   AltiVec Technology Programming Interface Manual
01538   
01539   MachineFunction &MF = DAG.getMachineFunction();
01540   MachineFrameInfo *MFI = MF.getFrameInfo();
01541 
01542   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01543   // Potential tail calls could cause overwriting of argument stack slots.
01544   bool isImmutable = !(PerformTailCallOpt && (CallConv==CallingConv::Fast));
01545   unsigned PtrByteSize = 4;
01546 
01547   // Assign locations to all of the incoming arguments.
01548   SmallVector<CCValAssign, 16> ArgLocs;
01549   CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
01550                  *DAG.getContext());
01551 
01552   // Reserve space for the linkage area on the stack.
01553   CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
01554 
01555   CCInfo.AnalyzeFormalArguments(Ins, CC_PPC_SVR4);
01556   
01557   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
01558     CCValAssign &VA = ArgLocs[i];
01559     
01560     // Arguments stored in registers.
01561     if (VA.isRegLoc()) {
01562       TargetRegisterClass *RC;
01563       EVT ValVT = VA.getValVT();
01564       
01565       switch (ValVT.getSimpleVT().SimpleTy) {
01566         default:
01567           llvm_unreachable("ValVT not supported by formal arguments Lowering");
01568         case MVT::i32:
01569           RC = PPC::GPRCRegisterClass;
01570           break;
01571         case MVT::f32:
01572           RC = PPC::F4RCRegisterClass;
01573           break;
01574         case MVT::f64:
01575           RC = PPC::F8RCRegisterClass;
01576           break;
01577         case MVT::v16i8:
01578         case MVT::v8i16:
01579         case MVT::v4i32:
01580         case MVT::v4f32:
01581           RC = PPC::VRRCRegisterClass;
01582           break;
01583       }
01584       
01585       // Transform the arguments stored in physical registers into virtual ones.
01586       unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
01587       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, ValVT);
01588 
01589       InVals.push_back(ArgValue);
01590     } else {
01591       // Argument stored in memory.
01592       assert(VA.isMemLoc());
01593 
01594       unsigned ArgSize = VA.getLocVT().getSizeInBits() / 8;
01595       int FI = MFI->CreateFixedObject(ArgSize, VA.getLocMemOffset(),
01596                                       isImmutable);
01597 
01598       // Create load nodes to retrieve arguments from the stack.
01599       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
01600       InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN, NULL, 0));
01601     }
01602   }
01603 
01604   // Assign locations to all of the incoming aggregate by value arguments.
01605   // Aggregates passed by value are stored in the local variable space of the
01606   // caller's stack frame, right above the parameter list area.
01607   SmallVector<CCValAssign, 16> ByValArgLocs;
01608   CCState CCByValInfo(CallConv, isVarArg, getTargetMachine(),
01609                       ByValArgLocs, *DAG.getContext());
01610 
01611   // Reserve stack space for the allocations in CCInfo.
01612   CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
01613 
01614   CCByValInfo.AnalyzeFormalArguments(Ins, CC_PPC_SVR4_ByVal);
01615 
01616   // Area that is at least reserved in the caller of this function.
01617   unsigned MinReservedArea = CCByValInfo.getNextStackOffset();
01618   
01619   // Set the size that is at least reserved in caller of this function.  Tail
01620   // call optimized function's reserved stack space needs to be aligned so that
01621   // taking the difference between two stack areas will result in an aligned
01622   // stack.
01623   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
01624 
01625   MinReservedArea =
01626     std::max(MinReservedArea,
01627              PPCFrameInfo::getMinCallFrameSize(false, false));
01628   
01629   unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
01630     getStackAlignment();
01631   unsigned AlignMask = TargetAlign-1;
01632   MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
01633   
01634   FI->setMinReservedArea(MinReservedArea);
01635 
01636   SmallVector<SDValue, 8> MemOps;
01637   
01638   // If the function takes variable number of arguments, make a frame index for
01639   // the start of the first vararg value... for expansion of llvm.va_start.
01640   if (isVarArg) {
01641     static const unsigned GPArgRegs[] = {
01642       PPC::R3, PPC::R4, PPC::R5, PPC::R6,
01643       PPC::R7, PPC::R8, PPC::R9, PPC::R10,
01644     };
01645     const unsigned NumGPArgRegs = array_lengthof(GPArgRegs);
01646 
01647     static const unsigned FPArgRegs[] = {
01648       PPC::F1, PPC::F2, PPC::F3, PPC::F4, PPC::F5, PPC::F6, PPC::F7,
01649       PPC::F8
01650     };
01651     const unsigned NumFPArgRegs = array_lengthof(FPArgRegs);
01652 
01653     VarArgsNumGPR = CCInfo.getFirstUnallocated(GPArgRegs, NumGPArgRegs);
01654     VarArgsNumFPR = CCInfo.getFirstUnallocated(FPArgRegs, NumFPArgRegs);
01655 
01656     // Make room for NumGPArgRegs and NumFPArgRegs.
01657     int Depth = NumGPArgRegs * PtrVT.getSizeInBits()/8 +
01658                 NumFPArgRegs * EVT(MVT::f64).getSizeInBits()/8;
01659 
01660     VarArgsStackOffset = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
01661                                                 CCInfo.getNextStackOffset());
01662 
01663     VarArgsFrameIndex = MFI->CreateStackObject(Depth, 8);
01664     SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
01665 
01666     // The fixed integer arguments of a variadic function are
01667     // stored to the VarArgsFrameIndex on the stack.
01668     unsigned GPRIndex = 0;
01669     for (; GPRIndex != VarArgsNumGPR; ++GPRIndex) {
01670       SDValue Val = DAG.getRegister(GPArgRegs[GPRIndex], PtrVT);
01671       SDValue Store = DAG.getStore(Chain, dl, Val, FIN, NULL, 0);
01672       MemOps.push_back(Store);
01673       // Increment the address by four for the next argument to store
01674       SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
01675       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
01676     }
01677 
01678     // If this function is vararg, store any remaining integer argument regs
01679     // to their spots on the stack so that they may be loaded by deferencing the
01680     // result of va_next.
01681     for (; GPRIndex != NumGPArgRegs; ++GPRIndex) {
01682       unsigned VReg = MF.addLiveIn(GPArgRegs[GPRIndex], &PPC::GPRCRegClass);
01683 
01684       SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
01685       SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
01686       MemOps.push_back(Store);
01687       // Increment the address by four for the next argument to store
01688       SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
01689       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
01690     }
01691 
01692     // FIXME 32-bit SVR4: We only need to save FP argument registers if CR bit 6
01693     // is set.
01694     
01695     // The double arguments are stored to the VarArgsFrameIndex
01696     // on the stack.
01697     unsigned FPRIndex = 0;
01698     for (FPRIndex = 0; FPRIndex != VarArgsNumFPR; ++FPRIndex) {
01699       SDValue Val = DAG.getRegister(FPArgRegs[FPRIndex], MVT::f64);
01700       SDValue Store = DAG.getStore(Chain, dl, Val, FIN, NULL, 0);
01701       MemOps.push_back(Store);
01702       // Increment the address by eight for the next argument to store
01703       SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
01704                                          PtrVT);
01705       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
01706     }
01707 
01708     for (; FPRIndex != NumFPArgRegs; ++FPRIndex) {
01709       unsigned VReg = MF.addLiveIn(FPArgRegs[FPRIndex], &PPC::F8RCRegClass);
01710 
01711       SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::f64);
01712       SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
01713       MemOps.push_back(Store);
01714       // Increment the address by eight for the next argument to store
01715       SDValue PtrOff = DAG.getConstant(EVT(MVT::f64).getSizeInBits()/8,
01716                                          PtrVT);
01717       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
01718     }
01719   }
01720 
01721   if (!MemOps.empty())
01722     Chain = DAG.getNode(ISD::TokenFactor, dl,
01723                         MVT::Other, &MemOps[0], MemOps.size());
01724 
01725   return Chain;
01726 }
01727 
01728 SDValue
01729 PPCTargetLowering::LowerFormalArguments_Darwin(
01730                                       SDValue Chain,
01731                                       unsigned CallConv, bool isVarArg,
01732                                       const SmallVectorImpl<ISD::InputArg>
01733                                         &Ins,
01734                                       DebugLoc dl, SelectionDAG &DAG,
01735                                       SmallVectorImpl<SDValue> &InVals) {
01736   // TODO: add description of PPC stack frame format, or at least some docs.
01737   //
01738   MachineFunction &MF = DAG.getMachineFunction();
01739   MachineFrameInfo *MFI = MF.getFrameInfo();
01740 
01741   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
01742   bool isPPC64 = PtrVT == MVT::i64;
01743   // Potential tail calls could cause overwriting of argument stack slots.
01744   bool isImmutable = !(PerformTailCallOpt && (CallConv==CallingConv::Fast));
01745   unsigned PtrByteSize = isPPC64 ? 8 : 4;
01746 
01747   unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true);
01748   // Area that is at least reserved in caller of this function.
01749   unsigned MinReservedArea = ArgOffset;
01750 
01751   static const unsigned GPR_32[] = {           // 32-bit registers.
01752     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
01753     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
01754   };
01755   static const unsigned GPR_64[] = {           // 64-bit registers.
01756     PPC::X3, PPC::X4, PPC::X5, PPC::X6,
01757     PPC::X7, PPC::X8, PPC::X9, PPC::X10,
01758   };
01759 
01760   static const unsigned *FPR = GetFPR();
01761 
01762   static const unsigned VR[] = {
01763     PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
01764     PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
01765   };
01766 
01767   const unsigned Num_GPR_Regs = array_lengthof(GPR_32);
01768   const unsigned Num_FPR_Regs = 13;
01769   const unsigned Num_VR_Regs  = array_lengthof( VR);
01770 
01771   unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
01772 
01773   const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
01774 
01775   // In 32-bit non-varargs functions, the stack space for vectors is after the
01776   // stack space for non-vectors.  We do not use this space unless we have
01777   // too many vectors to fit in registers, something that only occurs in
01778   // constructed examples:), but we have to walk the arglist to figure
01779   // that out...for the pathological case, compute VecArgOffset as the
01780   // start of the vector parameter area.  Computing VecArgOffset is the
01781   // entire point of the following loop.
01782   unsigned VecArgOffset = ArgOffset;
01783   if (!isVarArg && !isPPC64) {
01784     for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e;
01785          ++ArgNo) {
01786       EVT ObjectVT = Ins[ArgNo].VT;
01787       unsigned ObjSize = ObjectVT.getSizeInBits()/8;
01788       ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
01789 
01790       if (Flags.isByVal()) {
01791         // ObjSize is the true size, ArgSize rounded up to multiple of regs.
01792         ObjSize = Flags.getByValSize();
01793         unsigned ArgSize =
01794                 ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
01795         VecArgOffset += ArgSize;
01796         continue;
01797       }
01798 
01799       switch(ObjectVT.getSimpleVT().SimpleTy) {
01800       default: llvm_unreachable("Unhandled argument type!");
01801       case MVT::i32:
01802       case MVT::f32:
01803         VecArgOffset += isPPC64 ? 8 : 4;
01804         break;
01805       case MVT::i64:  // PPC64
01806       case MVT::f64:
01807         VecArgOffset += 8;
01808         break;
01809       case MVT::v4f32:
01810       case MVT::v4i32:
01811       case MVT::v8i16:
01812       case MVT::v16i8:
01813         // Nothing to do, we're only looking at Nonvector args here.
01814         break;
01815       }
01816     }
01817   }
01818   // We've found where the vector parameter area in memory is.  Skip the
01819   // first 12 parameters; these don't use that memory.
01820   VecArgOffset = ((VecArgOffset+15)/16)*16;
01821   VecArgOffset += 12*16;
01822 
01823   // Add DAG nodes to load the arguments or copy them out of registers.  On
01824   // entry to a function on PPC, the arguments start after the linkage area,
01825   // although the first ones are often in registers.
01826 
01827   SmallVector<SDValue, 8> MemOps;
01828   unsigned nAltivecParamsAtEnd = 0;
01829   for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) {
01830     SDValue ArgVal;
01831     bool needsLoad = false;
01832     EVT ObjectVT = Ins[ArgNo].VT;
01833     unsigned ObjSize = ObjectVT.getSizeInBits()/8;
01834     unsigned ArgSize = ObjSize;
01835     ISD::ArgFlagsTy Flags = Ins[ArgNo].Flags;
01836 
01837     unsigned CurArgOffset = ArgOffset;
01838 
01839     // Varargs or 64 bit Altivec parameters are padded to a 16 byte boundary.
01840     if (ObjectVT==MVT::v4f32 || ObjectVT==MVT::v4i32 ||
01841         ObjectVT==MVT::v8i16 || ObjectVT==MVT::v16i8) {
01842       if (isVarArg || isPPC64) {
01843         MinReservedArea = ((MinReservedArea+15)/16)*16;
01844         MinReservedArea += CalculateStackSlotSize(ObjectVT,
01845                                                   Flags,
01846                                                   PtrByteSize);
01847       } else  nAltivecParamsAtEnd++;
01848     } else
01849       // Calculate min reserved area.
01850       MinReservedArea += CalculateStackSlotSize(Ins[ArgNo].VT,
01851                                                 Flags,
01852                                                 PtrByteSize);
01853 
01854     // FIXME the codegen can be much improved in some cases.
01855     // We do not have to keep everything in memory.
01856     if (Flags.isByVal()) {
01857       // ObjSize is the true size, ArgSize rounded up to multiple of registers.
01858       ObjSize = Flags.getByValSize();
01859       ArgSize = ((ObjSize + PtrByteSize - 1)/PtrByteSize) * PtrByteSize;
01860       // Objects of size 1 and 2 are right justified, everything else is
01861       // left justified.  This means the memory address is adjusted forwards.
01862       if (ObjSize==1 || ObjSize==2) {
01863         CurArgOffset = CurArgOffset + (4 - ObjSize);
01864       }
01865       // The value of the object is its address.
01866       int FI = MFI->CreateFixedObject(ObjSize, CurArgOffset);
01867       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
01868       InVals.push_back(FIN);
01869       if (ObjSize==1 || ObjSize==2) {
01870         if (GPR_idx != Num_GPR_Regs) {
01871           unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
01872           SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
01873           SDValue Store = DAG.getTruncStore(Val.getValue(1), dl, Val, FIN,
01874                                NULL, 0, ObjSize==1 ? MVT::i8 : MVT::i16 );
01875           MemOps.push_back(Store);
01876           ++GPR_idx;
01877         }
01878         
01879         ArgOffset += PtrByteSize;
01880         
01881         continue;
01882       }
01883       for (unsigned j = 0; j < ArgSize; j += PtrByteSize) {
01884         // Store whatever pieces of the object are in registers
01885         // to memory.  ArgVal will be address of the beginning of
01886         // the object.
01887         if (GPR_idx != Num_GPR_Regs) {
01888           unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
01889           int FI = MFI->CreateFixedObject(PtrByteSize, ArgOffset);
01890           SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
01891           SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
01892           SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
01893           MemOps.push_back(Store);
01894           ++GPR_idx;
01895           ArgOffset += PtrByteSize;
01896         } else {
01897           ArgOffset += ArgSize - (ArgOffset-CurArgOffset);
01898           break;
01899         }
01900       }
01901       continue;
01902     }
01903 
01904     switch (ObjectVT.getSimpleVT().SimpleTy) {
01905     default: llvm_unreachable("Unhandled argument type!");
01906     case MVT::i32:
01907       if (!isPPC64) {
01908         if (GPR_idx != Num_GPR_Regs) {
01909           unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
01910           ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
01911           ++GPR_idx;
01912         } else {
01913           needsLoad = true;
01914           ArgSize = PtrByteSize;
01915         }
01916         // All int arguments reserve stack space in the Darwin ABI.
01917         ArgOffset += PtrByteSize;
01918         break;
01919       }
01920       // FALLTHROUGH
01921     case MVT::i64:  // PPC64
01922       if (GPR_idx != Num_GPR_Regs) {
01923         unsigned VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
01924         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
01925 
01926         if (ObjectVT == MVT::i32) {
01927           // PPC64 passes i8, i16, and i32 values in i64 registers. Promote
01928           // value to MVT::i64 and then truncate to the correct register size.
01929           if (Flags.isSExt())
01930             ArgVal = DAG.getNode(ISD::AssertSext, dl, MVT::i64, ArgVal,
01931                                  DAG.getValueType(ObjectVT));
01932           else if (Flags.isZExt())
01933             ArgVal = DAG.getNode(ISD::AssertZext, dl, MVT::i64, ArgVal,
01934                                  DAG.getValueType(ObjectVT));
01935 
01936           ArgVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, ArgVal);
01937         }
01938 
01939         ++GPR_idx;
01940       } else {
01941         needsLoad = true;
01942         ArgSize = PtrByteSize;
01943       }
01944       // All int arguments reserve stack space in the Darwin ABI.
01945       ArgOffset += 8;
01946       break;
01947 
01948     case MVT::f32:
01949     case MVT::f64:
01950       // Every 4 bytes of argument space consumes one of the GPRs available for
01951       // argument passing.
01952       if (GPR_idx != Num_GPR_Regs) {
01953         ++GPR_idx;
01954         if (ObjSize == 8 && GPR_idx != Num_GPR_Regs && !isPPC64)
01955           ++GPR_idx;
01956       }
01957       if (FPR_idx != Num_FPR_Regs) {
01958         unsigned VReg;
01959 
01960         if (ObjectVT == MVT::f32)
01961           VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F4RCRegClass);
01962         else
01963           VReg = MF.addLiveIn(FPR[FPR_idx], &PPC::F8RCRegClass);
01964 
01965         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
01966         ++FPR_idx;
01967       } else {
01968         needsLoad = true;
01969       }
01970 
01971       // All FP arguments reserve stack space in the Darwin ABI.
01972       ArgOffset += isPPC64 ? 8 : ObjSize;
01973       break;
01974     case MVT::v4f32:
01975     case MVT::v4i32:
01976     case MVT::v8i16:
01977     case MVT::v16i8:
01978       // Note that vector arguments in registers don't reserve stack space,
01979       // except in varargs functions.
01980       if (VR_idx != Num_VR_Regs) {
01981         unsigned VReg = MF.addLiveIn(VR[VR_idx], &PPC::VRRCRegClass);
01982         ArgVal = DAG.getCopyFromReg(Chain, dl, VReg, ObjectVT);
01983         if (isVarArg) {
01984           while ((ArgOffset % 16) != 0) {
01985             ArgOffset += PtrByteSize;
01986             if (GPR_idx != Num_GPR_Regs)
01987               GPR_idx++;
01988           }
01989           ArgOffset += 16;
01990           GPR_idx = std::min(GPR_idx+4, Num_GPR_Regs); // FIXME correct for ppc64?
01991         }
01992         ++VR_idx;
01993       } else {
01994         if (!isVarArg && !isPPC64) {
01995           // Vectors go after all the nonvectors.
01996           CurArgOffset = VecArgOffset;
01997           VecArgOffset += 16;
01998         } else {
01999           // Vectors are aligned.
02000           ArgOffset = ((ArgOffset+15)/16)*16;
02001           CurArgOffset = ArgOffset;
02002           ArgOffset += 16;
02003         }
02004         needsLoad = true;
02005       }
02006       break;
02007     }
02008 
02009     // We need to load the argument to a virtual register if we determined above
02010     // that we ran out of physical registers of the appropriate type.
02011     if (needsLoad) {
02012       int FI = MFI->CreateFixedObject(ObjSize,
02013                                       CurArgOffset + (ArgSize - ObjSize),
02014                                       isImmutable);
02015       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
02016       ArgVal = DAG.getLoad(ObjectVT, dl, Chain, FIN, NULL, 0);
02017     }
02018 
02019     InVals.push_back(ArgVal);
02020   }
02021 
02022   // Set the size that is at least reserved in caller of this function.  Tail
02023   // call optimized function's reserved stack space needs to be aligned so that
02024   // taking the difference between two stack areas will result in an aligned
02025   // stack.
02026   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
02027   // Add the Altivec parameters at the end, if needed.
02028   if (nAltivecParamsAtEnd) {
02029     MinReservedArea = ((MinReservedArea+15)/16)*16;
02030     MinReservedArea += 16*nAltivecParamsAtEnd;
02031   }
02032   MinReservedArea =
02033     std::max(MinReservedArea,
02034              PPCFrameInfo::getMinCallFrameSize(isPPC64, true));
02035   unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
02036     getStackAlignment();
02037   unsigned AlignMask = TargetAlign-1;
02038   MinReservedArea = (MinReservedArea + AlignMask) & ~AlignMask;
02039   FI->setMinReservedArea(MinReservedArea);
02040 
02041   // If the function takes variable number of arguments, make a frame index for
02042   // the start of the first vararg value... for expansion of llvm.va_start.
02043   if (isVarArg) {
02044     int Depth = ArgOffset;
02045 
02046     VarArgsFrameIndex = MFI->CreateFixedObject(PtrVT.getSizeInBits()/8,
02047                                                Depth);
02048     SDValue FIN = DAG.getFrameIndex(VarArgsFrameIndex, PtrVT);
02049 
02050     // If this function is vararg, store any remaining integer argument regs
02051     // to their spots on the stack so that they may be loaded by deferencing the
02052     // result of va_next.
02053     for (; GPR_idx != Num_GPR_Regs; ++GPR_idx) {
02054       unsigned VReg;
02055       
02056       if (isPPC64)
02057         VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::G8RCRegClass);
02058       else
02059         VReg = MF.addLiveIn(GPR[GPR_idx], &PPC::GPRCRegClass);
02060 
02061       SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, PtrVT);
02062       SDValue Store = DAG.getStore(Val.getValue(1), dl, Val, FIN, NULL, 0);
02063       MemOps.push_back(Store);
02064       // Increment the address by four for the next argument to store
02065       SDValue PtrOff = DAG.getConstant(PtrVT.getSizeInBits()/8, PtrVT);
02066       FIN = DAG.getNode(ISD::ADD, dl, PtrOff.getValueType(), FIN, PtrOff);
02067     }
02068   }
02069 
02070   if (!MemOps.empty())
02071     Chain = DAG.getNode(ISD::TokenFactor, dl,
02072                         MVT::Other, &MemOps[0], MemOps.size());
02073 
02074   return Chain;
02075 }
02076 
02077 /// CalculateParameterAndLinkageAreaSize - Get the size of the paramter plus
02078 /// linkage area for the Darwin ABI.
02079 static unsigned
02080 CalculateParameterAndLinkageAreaSize(SelectionDAG &DAG,
02081                                      bool isPPC64,
02082                                      bool isVarArg,
02083                                      unsigned CC,
02084                                      const SmallVectorImpl<ISD::OutputArg>
02085                                        &Outs,
02086                                      unsigned &nAltivecParamsAtEnd) {
02087   // Count how many bytes are to be pushed on the stack, including the linkage
02088   // area, and parameter passing area.  We start with 24/48 bytes, which is
02089   // prereserved space for [SP][CR][LR][3 x unused].
02090   unsigned NumBytes = PPCFrameInfo::getLinkageSize(isPPC64, true);
02091   unsigned NumOps = Outs.size();
02092   unsigned PtrByteSize = isPPC64 ? 8 : 4;
02093 
02094   // Add up all the space actually used.
02095   // In 32-bit non-varargs calls, Altivec parameters all go at the end; usually
02096   // they all go in registers, but we must reserve stack space for them for
02097   // possible use by the caller.  In varargs or 64-bit calls, parameters are
02098   // assigned stack space in order, with padding so Altivec parameters are
02099   // 16-byte aligned.
02100   nAltivecParamsAtEnd = 0;
02101   for (unsigned i = 0; i != NumOps; ++i) {
02102     SDValue Arg = Outs[i].Val;
02103     ISD::ArgFlagsTy Flags = Outs[i].Flags;
02104     EVT ArgVT = Arg.getValueType();
02105     // Varargs Altivec parameters are padded to a 16 byte boundary.
02106     if (ArgVT==MVT::v4f32 || ArgVT==MVT::v4i32 ||
02107         ArgVT==MVT::v8i16 || ArgVT==MVT::v16i8) {
02108       if (!isVarArg && !isPPC64) {
02109         // Non-varargs Altivec parameters go after all the non-Altivec
02110         // parameters; handle those later so we know how much padding we need.
02111         nAltivecParamsAtEnd++;
02112         continue;
02113       }
02114       // Varargs and 64-bit Altivec parameters are padded to 16 byte boundary.
02115       NumBytes = ((NumBytes+15)/16)*16;
02116     }
02117     NumBytes += CalculateStackSlotSize(ArgVT, Flags, PtrByteSize);
02118   }
02119 
02120    // Allow for Altivec parameters at the end, if needed.
02121   if (nAltivecParamsAtEnd) {
02122     NumBytes = ((NumBytes+15)/16)*16;
02123     NumBytes += 16*nAltivecParamsAtEnd;
02124   }
02125 
02126   // The prolog code of the callee may store up to 8 GPR argument registers to
02127   // the stack, allowing va_start to index over them in memory if its varargs.
02128   // Because we cannot tell if this is needed on the caller side, we have to
02129   // conservatively assume that it is needed.  As such, make sure we have at
02130   // least enough stack space for the caller to store the 8 GPRs.
02131   NumBytes = std::max(NumBytes,
02132                       PPCFrameInfo::getMinCallFrameSize(isPPC64, true));
02133 
02134   // Tail call needs the stack to be aligned.
02135   if (CC==CallingConv::Fast && PerformTailCallOpt) {
02136     unsigned TargetAlign = DAG.getMachineFunction().getTarget().getFrameInfo()->
02137       getStackAlignment();
02138     unsigned AlignMask = TargetAlign-1;
02139     NumBytes = (NumBytes + AlignMask) & ~AlignMask;
02140   }
02141 
02142   return NumBytes;
02143 }
02144 
02145 /// CalculateTailCallSPDiff - Get the amount the stack pointer has to be
02146 /// adjusted to accomodate the arguments for the tailcall.
02147 static int CalculateTailCallSPDiff(SelectionDAG& DAG, bool IsTailCall,
02148                                    unsigned ParamSize) {
02149 
02150   if (!IsTailCall) return 0;
02151 
02152   PPCFunctionInfo *FI = DAG.getMachineFunction().getInfo<PPCFunctionInfo>();
02153   unsigned CallerMinReservedArea = FI->getMinReservedArea();
02154   int SPDiff = (int)CallerMinReservedArea - (int)ParamSize;
02155   // Remember only if the new adjustement is bigger.
02156   if (SPDiff < FI->getTailCallSPDelta())
02157     FI->setTailCallSPDelta(SPDiff);
02158 
02159   return SPDiff;
02160 }
02161 
02162 /// IsEligibleForTailCallOptimization - Check whether the call is eligible
02163 /// for tail call optimization. Targets which want to do tail call
02164 /// optimization should implement this function.
02165 bool
02166 PPCTargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
02167                                                      unsigned CalleeCC,
02168                                                      bool isVarArg,
02169                                       const SmallVectorImpl<ISD::InputArg> &Ins,
02170                                                      SelectionDAG& DAG) const {
02171   // Variable argument functions are not supported.
02172   if (isVarArg)
02173     return false;
02174 
02175   MachineFunction &MF = DAG.getMachineFunction();
02176   unsigned CallerCC = MF.getFunction()->getCallingConv();
02177   if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
02178     // Functions containing by val parameters are not supported.
02179     for (unsigned i = 0; i != Ins.size(); i++) {
02180        ISD::ArgFlagsTy Flags = Ins[i].Flags;
02181        if (Flags.isByVal()) return false;
02182     }
02183 
02184     // Non PIC/GOT  tail calls are supported.
02185     if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
02186       return true;
02187 
02188     // At the moment we can only do local tail calls (in same module, hidden
02189     // or protected) if we are generating PIC.
02190     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
02191       return G->getGlobal()->hasHiddenVisibility()
02192           || G->getGlobal()->hasProtectedVisibility();
02193   }
02194 
02195   return false;
02196 }
02197 
02198 /// isCallCompatibleAddress - Return the immediate to use if the specified
02199 /// 32-bit value is representable in the immediate field of a BxA instruction.
02200 static SDNode *isBLACompatibleAddress(SDValue Op, SelectionDAG &DAG) {
02201   ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op);
02202   if (!C) return 0;
02203 
02204   int Addr = C->getZExtValue();
02205   if ((Addr & 3) != 0 ||  // Low 2 bits are implicitly zero.
02206       (Addr << 6 >> 6) != Addr)
02207     return 0;  // Top 6 bits have to be sext of immediate.
02208 
02209   return DAG.getConstant((int)C->getZExtValue() >> 2,
02210                          DAG.getTargetLoweringInfo().getPointerTy()).getNode();
02211 }
02212 
02213 namespace {
02214 
02215 struct TailCallArgumentInfo {
02216   SDValue Arg;
02217   SDValue FrameIdxOp;
02218   int       FrameIdx;
02219 
02220   TailCallArgumentInfo() : FrameIdx(0) {}
02221 };
02222 
02223 }
02224 
02225 /// StoreTailCallArgumentsToStackSlot - Stores arguments to their stack slot.
02226 static void
02227 StoreTailCallArgumentsToStackSlot(SelectionDAG &DAG,
02228                                            SDValue Chain,
02229                    const SmallVector<TailCallArgumentInfo, 8> &TailCallArgs,
02230                    SmallVector<SDValue, 8> &MemOpChains,
02231                    DebugLoc dl) {
02232   for (unsigned i = 0, e = TailCallArgs.size(); i != e; ++i) {
02233     SDValue Arg = TailCallArgs[i].Arg;
02234     SDValue FIN = TailCallArgs[i].FrameIdxOp;
02235     int FI = TailCallArgs[i].FrameIdx;
02236     // Store relative to framepointer.
02237     MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, FIN,
02238                                        PseudoSourceValue::getFixedStack(FI),
02239                                        0));
02240   }
02241 }
02242 
02243 /// EmitTailCallStoreFPAndRetAddr - Move the frame pointer and return address to
02244 /// the appropriate stack slot for the tail call optimized function call.
02245 static SDValue EmitTailCallStoreFPAndRetAddr(SelectionDAG &DAG,
02246                                                MachineFunction &MF,
02247                                                SDValue Chain,
02248                                                SDValue OldRetAddr,
02249                                                SDValue OldFP,
02250                                                int SPDiff,
02251                                                bool isPPC64,
02252                                                bool isDarwinABI,
02253                                                DebugLoc dl) {
02254   if (SPDiff) {
02255     // Calculate the new stack slot for the return address.
02256     int SlotSize = isPPC64 ? 8 : 4;
02257     int NewRetAddrLoc = SPDiff + PPCFrameInfo::getReturnSaveOffset(isPPC64,
02258                                                                    isDarwinABI);
02259     int NewRetAddr = MF.getFrameInfo()->CreateFixedObject(SlotSize,
02260                                                           NewRetAddrLoc);
02261     EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
02262     SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewRetAddr, VT);
02263     Chain = DAG.getStore(Chain, dl, OldRetAddr, NewRetAddrFrIdx,
02264                          PseudoSourceValue::getFixedStack(NewRetAddr), 0);
02265 
02266     // When using the 32/64-bit SVR4 ABI there is no need to move the FP stack
02267     // slot as the FP is never overwritten.
02268     if (isDarwinABI) {
02269       int NewFPLoc =
02270         SPDiff + PPCFrameInfo::getFramePointerSaveOffset(isPPC64, isDarwinABI);
02271       int NewFPIdx = MF.getFrameInfo()->CreateFixedObject(SlotSize, NewFPLoc);
02272       SDValue NewFramePtrIdx = DAG.getFrameIndex(NewFPIdx, VT);
02273       Chain = DAG.getStore(Chain, dl, OldFP, NewFramePtrIdx,
02274                            PseudoSourceValue::getFixedStack(NewFPIdx), 0);
02275     }
02276   }
02277   return Chain;
02278 }
02279 
02280 /// CalculateTailCallArgDest - Remember Argument for later processing. Calculate
02281 /// the position of the argument.
02282 static void
02283 CalculateTailCallArgDest(SelectionDAG &DAG, MachineFunction &MF, bool isPPC64,
02284                          SDValue Arg, int SPDiff, unsigned ArgOffset,
02285                       SmallVector<TailCallArgumentInfo, 8>& TailCallArguments) {
02286   int Offset = ArgOffset + SPDiff;
02287   uint32_t OpSize = (Arg.getValueType().getSizeInBits()+7)/8;
02288   int FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
02289   EVT VT = isPPC64 ? MVT::i64 : MVT::i32;
02290   SDValue FIN = DAG.getFrameIndex(FI, VT);
02291   TailCallArgumentInfo Info;
02292   Info.Arg = Arg;
02293   Info.FrameIdxOp = FIN;
02294   Info.FrameIdx = FI;
02295   TailCallArguments.push_back(Info);
02296 }
02297 
02298 /// EmitTCFPAndRetAddrLoad - Emit load from frame pointer and return address
02299 /// stack slot. Returns the chain as result and the loaded frame pointers in
02300 /// LROpOut/FPOpout. Used when tail calling.
02301 SDValue PPCTargetLowering::EmitTailCallLoadFPAndRetAddr(SelectionDAG & DAG,
02302                                                         int SPDiff,
02303                                                         SDValue Chain,
02304                                                         SDValue &LROpOut,
02305                                                         SDValue &FPOpOut,
02306                                                         bool isDarwinABI,
02307                                                         DebugLoc dl) {
02308   if (SPDiff) {
02309     // Load the LR and FP stack slot for later adjusting.
02310     EVT VT = PPCSubTarget.isPPC64() ? MVT::i64 : MVT::i32;
02311     LROpOut = getReturnAddrFrameIndex(DAG);
02312     LROpOut = DAG.getLoad(VT, dl, Chain, LROpOut, NULL, 0);
02313     Chain = SDValue(LROpOut.getNode(), 1);
02314     
02315     // When using the 32/64-bit SVR4 ABI there is no need to load the FP stack
02316     // slot as the FP is never overwritten.
02317     if (isDarwinABI) {
02318       FPOpOut = getFramePointerFrameIndex(DAG);
02319       FPOpOut = DAG.getLoad(VT, dl, Chain, FPOpOut, NULL, 0);
02320       Chain = SDValue(FPOpOut.getNode(), 1);
02321     }
02322   }
02323   return Chain;
02324 }
02325 
02326 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
02327 /// by "Src" to address "Dst" of size "Size".  Alignment information is
02328 /// specified by the specific parameter attribute. The copy will be passed as
02329 /// a byval function parameter.
02330 /// Sometimes what we are copying is the end of a larger object, the part that
02331 /// does not fit in registers.
02332 static SDValue
02333 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
02334                           ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
02335                           DebugLoc dl) {
02336   SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
02337   return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
02338                        false, NULL, 0, NULL, 0);
02339 }
02340 
02341 /// LowerMemOpCallTo - Store the argument to the stack or remember it in case of
02342 /// tail calls.
02343 static void
02344 LowerMemOpCallTo(SelectionDAG &DAG, MachineFunction &MF, SDValue Chain,
02345                  SDValue Arg, SDValue PtrOff, int SPDiff,
02346                  unsigned ArgOffset, bool isPPC64, bool isTailCall,
02347                  bool isVector, SmallVector<SDValue, 8> &MemOpChains,
02348                  SmallVector<TailCallArgumentInfo, 8>& TailCallArguments,
02349                  DebugLoc dl) {
02350   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
02351   if (!isTailCall) {
02352     if (isVector) {
02353       SDValue StackPtr;
02354       if (isPPC64)
02355         StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
02356       else
02357         StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
02358       PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
02359                            DAG.getConstant(ArgOffset, PtrVT));
02360     }
02361     MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0));
02362   // Calculate and remember argument location.
02363   } else CalculateTailCallArgDest(DAG, MF, isPPC64, Arg, SPDiff, ArgOffset,
02364                                   TailCallArguments);
02365 }
02366 
02367 static
02368 void PrepareTailCall(SelectionDAG &DAG, SDValue &InFlag, SDValue &Chain,
02369                      DebugLoc dl, bool isPPC64, int SPDiff, unsigned NumBytes,
02370                      SDValue LROp, SDValue FPOp, bool isDarwinABI,
02371                      SmallVector<TailCallArgumentInfo, 8> &TailCallArguments) {
02372   MachineFunction &MF = DAG.getMachineFunction();
02373 
02374   // Emit a sequence of copyto/copyfrom virtual registers for arguments that
02375   // might overwrite each other in case of tail call optimization.
02376   SmallVector<SDValue, 8> MemOpChains2;
02377   // Do not flag preceeding copytoreg stuff together with the following stuff.
02378   InFlag = SDValue();
02379   StoreTailCallArgumentsToStackSlot(DAG, Chain, TailCallArguments,
02380                                     MemOpChains2, dl);
02381   if (!MemOpChains2.empty())
02382     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
02383                         &MemOpChains2[0], MemOpChains2.size());
02384 
02385   // Store the return address to the appropriate stack slot.
02386   Chain = EmitTailCallStoreFPAndRetAddr(DAG, MF, Chain, LROp, FPOp, SPDiff,
02387                                         isPPC64, isDarwinABI, dl);
02388 
02389   // Emit callseq_end just before tailcall node.
02390   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
02391                              DAG.getIntPtrConstant(0, true), InFlag);
02392   InFlag = Chain.getValue(1);
02393 }
02394 
02395 static
02396 unsigned PrepareCall(SelectionDAG &DAG, SDValue &Callee, SDValue &InFlag,
02397                      SDValue &Chain, DebugLoc dl, int SPDiff, bool isTailCall,
02398                      SmallVector<std::pair<unsigned, SDValue>, 8> &RegsToPass,
02399                      SmallVector<SDValue, 8> &Ops, std::vector<EVT> &NodeTys,
02400                      bool isSVR4ABI) {
02401   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
02402   NodeTys.push_back(MVT::Other);   // Returns a chain
02403   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
02404 
02405   unsigned CallOpc = isSVR4ABI ? PPCISD::CALL_SVR4 : PPCISD::CALL_Darwin;
02406 
02407   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
02408   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
02409   // node so that legalize doesn't hack it.
02410   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
02411     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), Callee.getValueType());
02412   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
02413     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), Callee.getValueType());
02414   else if (SDNode *Dest = isBLACompatibleAddress(Callee, DAG))
02415     // If this is an absolute destination address, use the munged value.
02416     Callee = SDValue(Dest, 0);
02417   else {
02418     // Otherwise, this is an indirect call.  We have to use a MTCTR/BCTRL pair
02419     // to do the call, we can't use PPCISD::CALL.
02420     SDValue MTCTROps[] = {Chain, Callee, InFlag};
02421     Chain = DAG.getNode(PPCISD::MTCTR, dl, NodeTys, MTCTROps,
02422                         2 + (InFlag.getNode() != 0));
02423     InFlag = Chain.getValue(1);
02424 
02425     NodeTys.clear();
02426     NodeTys.push_back(MVT::Other);
02427     NodeTys.push_back(MVT::Flag);
02428     Ops.push_back(Chain);
02429     CallOpc = isSVR4ABI ? PPCISD::BCTRL_SVR4 : PPCISD::BCTRL_Darwin;
02430     Callee.setNode(0);
02431     // Add CTR register as callee so a bctr can be emitted later.
02432     if (isTailCall)
02433       Ops.push_back(DAG.getRegister(PPC::CTR, PtrVT));
02434   }
02435 
02436   // If this is a direct call, pass the chain and the callee.
02437   if (Callee.getNode()) {
02438     Ops.push_back(Chain);
02439     Ops.push_back(Callee);
02440   }
02441   // If this is a tail call add stack pointer delta.
02442   if (isTailCall)
02443     Ops.push_back(DAG.getConstant(SPDiff, MVT::i32));
02444 
02445   // Add argument registers to the end of the list so that they are known live
02446   // into the call.
02447   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
02448     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
02449                                   RegsToPass[i].second.getValueType()));
02450 
02451   return CallOpc;
02452 }
02453 
02454 SDValue
02455 PPCTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
02456                                    unsigned CallConv, bool isVarArg,
02457                                    const SmallVectorImpl<ISD::InputArg> &Ins,
02458                                    DebugLoc dl, SelectionDAG &DAG,
02459                                    SmallVectorImpl<SDValue> &InVals) {
02460 
02461   SmallVector<CCValAssign, 16> RVLocs;
02462   CCState CCRetInfo(CallConv, isVarArg, getTargetMachine(),
02463                     RVLocs, *DAG.getContext());
02464   CCRetInfo.AnalyzeCallResult(Ins, RetCC_PPC);
02465 
02466   // Copy all of the result registers out of their specified physreg.
02467   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
02468     CCValAssign &VA = RVLocs[i];
02469     EVT VT = VA.getValVT();
02470     assert(VA.isRegLoc() && "Can only return in registers!");
02471     Chain = DAG.getCopyFromReg(Chain, dl,
02472                                VA.getLocReg(), VT, InFlag).getValue(1);
02473     InVals.push_back(Chain.getValue(0));
02474     InFlag = Chain.getValue(2);
02475   }
02476 
02477   return Chain;
02478 }
02479 
02480 SDValue
02481 PPCTargetLowering::FinishCall(unsigned CallConv, DebugLoc dl, bool isTailCall,
02482                               bool isVarArg,
02483                               SelectionDAG &DAG,
02484                               SmallVector<std::pair<unsigned, SDValue>, 8>
02485                                 &RegsToPass,
02486                               SDValue InFlag, SDValue Chain,
02487                               SDValue &Callee,
02488                               int SPDiff, unsigned NumBytes,
02489                               const SmallVectorImpl<ISD::InputArg> &Ins,
02490                               SmallVectorImpl<SDValue> &InVals) {
02491   std::vector<EVT> NodeTys;
02492   SmallVector<SDValue, 8> Ops;
02493   unsigned CallOpc = PrepareCall(DAG, Callee, InFlag, Chain, dl, SPDiff,
02494                                  isTailCall, RegsToPass, Ops, NodeTys,
02495                                  PPCSubTarget.isSVR4ABI());
02496 
02497   // When performing tail call optimization the callee pops its arguments off
02498   // the stack. Account for this here so these bytes can be pushed back on in
02499   // PPCRegisterInfo::eliminateCallFramePseudoInstr.
02500   int BytesCalleePops =
02501     (CallConv==CallingConv::Fast && PerformTailCallOpt) ? NumBytes : 0;
02502 
02503   if (InFlag.getNode())
02504     Ops.push_back(InFlag);
02505 
02506   // Emit tail call.
02507   if (isTailCall) {
02508     // If this is the first return lowered for this function, add the regs
02509     // to the liveout set for the function.
02510     if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
02511       SmallVector<CCValAssign, 16> RVLocs;
02512       CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
02513                      *DAG.getContext());
02514       CCInfo.AnalyzeCallResult(Ins, RetCC_PPC);
02515       for (unsigned i = 0; i != RVLocs.size(); ++i)
02516         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
02517     }
02518 
02519     assert(((Callee.getOpcode() == ISD::Register &&
02520              cast<RegisterSDNode>(Callee)->getReg() == PPC::CTR) ||
02521             Callee.getOpcode() == ISD::TargetExternalSymbol ||
02522             Callee.getOpcode() == ISD::TargetGlobalAddress ||
02523             isa<ConstantSDNode>(Callee)) &&
02524     "Expecting an global address, external symbol, absolute value or register");
02525 
02526     return DAG.getNode(PPCISD::TC_RETURN, dl, MVT::Other, &Ops[0], Ops.size());
02527   }
02528 
02529   Chain = DAG.getNode(CallOpc, dl, NodeTys, &Ops[0], Ops.size());
02530   InFlag = Chain.getValue(1);
02531 
02532   // Add a NOP immediately after the branch instruction when using the 64-bit
02533   // SVR4 ABI. At link time, if caller and callee are in a different module and
02534   // thus have a different TOC, the call will be replaced with a call to a stub
02535   // function which saves the current TOC, loads the TOC of the callee and
02536   // branches to the callee. The NOP will be replaced with a load instruction
02537   // which restores the TOC of the caller from the TOC save slot of the current
02538   // stack frame. If caller and callee belong to the same module (and have the
02539   // same TOC), the NOP will remain unchanged.
02540   if (!isTailCall && PPCSubTarget.isSVR4ABI()&& PPCSubTarget.isPPC64()) {
02541     // Insert NOP.
02542     InFlag = DAG.getNode(PPCISD::NOP, dl, MVT::Flag, InFlag);
02543   }
02544 
02545   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
02546                              DAG.getIntPtrConstant(BytesCalleePops, true),
02547                              InFlag);
02548   if (!Ins.empty())
02549     InFlag = Chain.getValue(1);
02550 
02551   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
02552                          Ins, dl, DAG, InVals);
02553 }
02554 
02555 SDValue
02556 PPCTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
02557                              unsigned CallConv, bool isVarArg,
02558                              bool isTailCall,
02559                              const SmallVectorImpl<ISD::OutputArg> &Outs,
02560                              const SmallVectorImpl<ISD::InputArg> &Ins,
02561                              DebugLoc dl, SelectionDAG &DAG,
02562                              SmallVectorImpl<SDValue> &InVals) {
02563   if (PPCSubTarget.isSVR4ABI() && !PPCSubTarget.isPPC64()) {
02564     return LowerCall_SVR4(Chain, Callee, CallConv, isVarArg,
02565                           isTailCall, Outs, Ins,
02566                           dl, DAG, InVals);
02567   } else {
02568     return LowerCall_Darwin(Chain, Callee, CallConv, isVarArg,
02569                             isTailCall, Outs, Ins,
02570                             dl, DAG, InVals);
02571   }
02572 }
02573 
02574 SDValue
02575 PPCTargetLowering::LowerCall_SVR4(SDValue Chain, SDValue Callee,
02576                                   unsigned CallConv, bool isVarArg,
02577                                   bool isTailCall,
02578                                   const SmallVectorImpl<ISD::OutputArg> &Outs,
02579                                   const SmallVectorImpl<ISD::InputArg> &Ins,
02580                                   DebugLoc dl, SelectionDAG &DAG,
02581                                   SmallVectorImpl<SDValue> &InVals) {
02582   // See PPCTargetLowering::LowerFormalArguments_SVR4() for a description
02583   // of the 32-bit SVR4 ABI stack frame layout.
02584 
02585   assert((!isTailCall ||
02586           (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
02587          "IsEligibleForTailCallOptimization missed a case!");
02588 
02589   assert((CallConv == CallingConv::C ||
02590           CallConv == CallingConv::Fast) && "Unknown calling convention!");
02591 
02592   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
02593   unsigned PtrByteSize = 4;
02594 
02595   MachineFunction &MF = DAG.getMachineFunction();
02596 
02597   // Mark this function as potentially containing a function that contains a
02598   // tail call. As a consequence the frame pointer will be used for dynamicalloc
02599   // and restoring the callers stack pointer in this functions epilog. This is
02600   // done because by tail calling the called function might overwrite the value
02601   // in this function's (MF) stack pointer stack slot 0(SP).
02602   if (PerformTailCallOpt && CallConv==CallingConv::Fast)
02603     MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
02604   
02605   // Count how many bytes are to be pushed on the stack, including the linkage
02606   // area, parameter list area and the part of the local variable space which
02607   // contains copies of aggregates which are passed by value.
02608 
02609   // Assign locations to all of the outgoing arguments.
02610   SmallVector<CCValAssign, 16> ArgLocs;
02611   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
02612                  ArgLocs, *DAG.getContext());
02613 
02614   // Reserve space for the linkage area on the stack.
02615   CCInfo.AllocateStack(PPCFrameInfo::getLinkageSize(false, false), PtrByteSize);
02616 
02617   if (isVarArg) {
02618     // Handle fixed and variable vector arguments differently.
02619     // Fixed vector arguments go into registers as long as registers are
02620     // available. Variable vector arguments always go into memory.
02621     unsigned NumArgs = Outs.size();
02622     
02623     for (unsigned i = 0; i != NumArgs; ++i) {
02624       EVT ArgVT = Outs[i].Val.getValueType();
02625       ISD::ArgFlagsTy ArgFlags = Outs[i].Flags;
02626       bool Result;
02627       
02628       if (Outs[i].IsFixed) {
02629         Result = CC_PPC_SVR4(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags,
02630                              CCInfo);
02631       } else {
02632         Result = CC_PPC_SVR4_VarArg(i, ArgVT, ArgVT, CCValAssign::Full,
02633                                     ArgFlags, CCInfo);
02634       }
02635       
02636       if (Result) {
02637 #ifndef NDEBUG
02638         cerr << "Call operand #" << i << " has unhandled type "
02639              << ArgVT.getEVTString() << "\n";
02640 #endif
02641         llvm_unreachable(0);
02642       }
02643     }
02644   } else {
02645     // All arguments are treated the same.
02646     CCInfo.AnalyzeCallOperands(Outs, CC_PPC_SVR4);
02647   }
02648   
02649   // Assign locations to all of the outgoing aggregate by value arguments.
02650   SmallVector<CCValAssign, 16> ByValArgLocs;
02651   CCState CCByValInfo(CallConv, isVarArg, getTargetMachine(), ByValArgLocs,
02652                       *DAG.getContext());
02653 
02654   // Reserve stack space for the allocations in CCInfo.
02655   CCByValInfo.AllocateStack(CCInfo.getNextStackOffset(), PtrByteSize);
02656 
02657   CCByValInfo.AnalyzeCallOperands(Outs, CC_PPC_SVR4_ByVal);
02658 
02659   // Size of the linkage area, parameter list area and the part of the local
02660   // space variable where copies of aggregates which are passed by value are
02661   // stored.
02662   unsigned NumBytes = CCByValInfo.getNextStackOffset();
02663   
02664   // Calculate by how many bytes the stack has to be adjusted in case of tail
02665   // call optimization.
02666   int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
02667 
02668   // Adjust the stack pointer for the new arguments...
02669   // These operations are automatically eliminated by the prolog/epilog pass
02670   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
02671   SDValue CallSeqStart = Chain;
02672 
02673   // Load the return address and frame pointer so it can be moved somewhere else
02674   // later.
02675   SDValue LROp, FPOp;
02676   Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, false,
02677                                        dl);
02678 
02679   // Set up a copy of the stack pointer for use loading and storing any
02680   // arguments that may not fit in the registers available for argument
02681   // passing.
02682   SDValue StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
02683   
02684   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
02685   SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
02686   SmallVector<SDValue, 8> MemOpChains;
02687 
02688   // Walk the register/memloc assignments, inserting copies/loads.
02689   for (unsigned i = 0, j = 0, e = ArgLocs.size();
02690        i != e;
02691        ++i) {
02692     CCValAssign &VA = ArgLocs[i];
02693     SDValue Arg = Outs[i].Val;
02694     ISD::ArgFlagsTy Flags = Outs[i].Flags;
02695     
02696     if (Flags.isByVal()) {
02697       // Argument is an aggregate which is passed by value, thus we need to
02698       // create a copy of it in the local variable space of the current stack
02699       // frame (which is the stack frame of the caller) and pass the address of
02700       // this copy to the callee.
02701       assert((j < ByValArgLocs.size()) && "Index out of bounds!");
02702       CCValAssign &ByValVA = ByValArgLocs[j++];
02703       assert((VA.getValNo() == ByValVA.getValNo()) && "ValNo mismatch!");
02704       
02705       // Memory reserved in the local variable space of the callers stack frame.
02706       unsigned LocMemOffset = ByValVA.getLocMemOffset();
02707       
02708       SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
02709       PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
02710       
02711       // Create a copy of the argument in the local area of the current
02712       // stack frame.
02713       SDValue MemcpyCall =
02714         CreateCopyOfByValArgument(Arg, PtrOff,
02715                                   CallSeqStart.getNode()->getOperand(0),
02716                                   Flags, DAG, dl);
02717       
02718       // This must go outside the CALLSEQ_START..END.
02719       SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
02720                            CallSeqStart.getNode()->getOperand(1));
02721       DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
02722                              NewCallSeqStart.getNode());
02723       Chain = CallSeqStart = NewCallSeqStart;
02724       
02725       // Pass the address of the aggregate copy on the stack either in a
02726       // physical register or in the parameter list area of the current stack
02727       // frame to the callee.
02728       Arg = PtrOff;
02729     }
02730     
02731     if (VA.isRegLoc()) {
02732       // Put argument in a physical register.
02733       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
02734     } else {
02735       // Put argument in the parameter list area of the current stack frame.
02736       assert(VA.isMemLoc());
02737       unsigned LocMemOffset = VA.getLocMemOffset();
02738 
02739       if (!isTailCall) {
02740         SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
02741         PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
02742 
02743         MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
02744                               PseudoSourceValue::getStack(), LocMemOffset));
02745       } else {
02746         // Calculate and remember argument location.
02747         CalculateTailCallArgDest(DAG, MF, false, Arg, SPDiff, LocMemOffset,
02748                                  TailCallArguments);
02749       }
02750     }
02751   }
02752   
02753   if (!MemOpChains.empty())
02754     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
02755                         &MemOpChains[0], MemOpChains.size());
02756   
02757   // Build a sequence of copy-to-reg nodes chained together with token chain
02758   // and flag operands which copy the outgoing args into the appropriate regs.
02759   SDValue InFlag;
02760   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
02761     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
02762                              RegsToPass[i].second, InFlag);
02763     InFlag = Chain.getValue(1);
02764   }
02765   
02766   // Set CR6 to true if this is a vararg call.
02767   if (isVarArg) {
02768     SDValue SetCR(DAG.getTargetNode(PPC::CRSET, dl, MVT::i32), 0);
02769     Chain = DAG.getCopyToReg(Chain, dl, PPC::CR1EQ, SetCR, InFlag);
02770     InFlag = Chain.getValue(1);
02771   }
02772 
02773   if (isTailCall) {
02774     PrepareTailCall(DAG, InFlag, Chain, dl, false, SPDiff, NumBytes, LROp, FPOp,
02775                     false, TailCallArguments);
02776   }
02777 
02778   return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
02779                     RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
02780                     Ins, InVals);
02781 }
02782 
02783 SDValue
02784 PPCTargetLowering::LowerCall_Darwin(SDValue Chain, SDValue Callee,
02785                                     unsigned CallConv, bool isVarArg,
02786                                     bool isTailCall,
02787                                     const SmallVectorImpl<ISD::OutputArg> &Outs,
02788                                     const SmallVectorImpl<ISD::InputArg> &Ins,
02789                                     DebugLoc dl, SelectionDAG &DAG,
02790                                     SmallVectorImpl<SDValue> &InVals) {
02791 
02792   unsigned NumOps  = Outs.size();
02793 
02794   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
02795   bool isPPC64 = PtrVT == MVT::i64;
02796   unsigned PtrByteSize = isPPC64 ? 8 : 4;
02797 
02798   MachineFunction &MF = DAG.getMachineFunction();
02799 
02800   // Mark this function as potentially containing a function that contains a
02801   // tail call. As a consequence the frame pointer will be used for dynamicalloc
02802   // and restoring the callers stack pointer in this functions epilog. This is
02803   // done because by tail calling the called function might overwrite the value
02804   // in this function's (MF) stack pointer stack slot 0(SP).
02805   if (PerformTailCallOpt && CallConv==CallingConv::Fast)
02806     MF.getInfo<PPCFunctionInfo>()->setHasFastCall();
02807 
02808   unsigned nAltivecParamsAtEnd = 0;
02809 
02810   // Count how many bytes are to be pushed on the stack, including the linkage
02811   // area, and parameter passing area.  We start with 24/48 bytes, which is
02812   // prereserved space for [SP][CR][LR][3 x unused].
02813   unsigned NumBytes =
02814     CalculateParameterAndLinkageAreaSize(DAG, isPPC64, isVarArg, CallConv,
02815                                          Outs,
02816                                          nAltivecParamsAtEnd);
02817 
02818   // Calculate by how many bytes the stack has to be adjusted in case of tail
02819   // call optimization.
02820   int SPDiff = CalculateTailCallSPDiff(DAG, isTailCall, NumBytes);
02821 
02822   // To protect arguments on the stack from being clobbered in a tail call,
02823   // force all the loads to happen before doing any other lowering.
02824   if (isTailCall)
02825     Chain = DAG.getStackArgumentTokenFactor(Chain);
02826 
02827   // Adjust the stack pointer for the new arguments...
02828   // These operations are automatically eliminated by the prolog/epilog pass
02829   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
02830   SDValue CallSeqStart = Chain;
02831 
02832   // Load the return address and frame pointer so it can be move somewhere else
02833   // later.
02834   SDValue LROp, FPOp;
02835   Chain = EmitTailCallLoadFPAndRetAddr(DAG, SPDiff, Chain, LROp, FPOp, true,
02836                                        dl);
02837 
02838   // Set up a copy of the stack pointer for use loading and storing any
02839   // arguments that may not fit in the registers available for argument
02840   // passing.
02841   SDValue StackPtr;
02842   if (isPPC64)
02843     StackPtr = DAG.getRegister(PPC::X1, MVT::i64);
02844   else
02845     StackPtr = DAG.getRegister(PPC::R1, MVT::i32);
02846 
02847   // Figure out which arguments are going to go in registers, and which in
02848   // memory.  Also, if this is a vararg function, floating point operations
02849   // must be stored to our stack, and loaded into integer regs as well, if
02850   // any integer regs are available for argument passing.
02851   unsigned ArgOffset = PPCFrameInfo::getLinkageSize(isPPC64, true);
02852   unsigned GPR_idx = 0, FPR_idx = 0, VR_idx = 0;
02853 
02854   static const unsigned GPR_32[] = {           // 32-bit registers.
02855     PPC::R3, PPC::R4, PPC::R5, PPC::R6,
02856     PPC::R7, PPC::R8, PPC::R9, PPC::R10,
02857   };
02858   static const unsigned GPR_64[] = {           // 64-bit registers.
02859     PPC::X3, PPC::X4, PPC::X5, PPC::X6,
02860     PPC::X7, PPC::X8, PPC::X9, PPC::X10,
02861   };
02862   static const unsigned *FPR = GetFPR();
02863 
02864   static const unsigned VR[] = {
02865     PPC::V2, PPC::V3, PPC::V4, PPC::V5, PPC::V6, PPC::V7, PPC::V8,
02866     PPC::V9, PPC::V10, PPC::V11, PPC::V12, PPC::V13
02867   };
02868   const unsigned NumGPRs = array_lengthof(GPR_32);
02869   const unsigned NumFPRs = 13;
02870   const unsigned NumVRs  = array_lengthof(VR);
02871 
02872   const unsigned *GPR = isPPC64 ? GPR_64 : GPR_32;
02873 
02874   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
02875   SmallVector<TailCallArgumentInfo, 8> TailCallArguments;
02876 
02877   SmallVector<SDValue, 8> MemOpChains;
02878   for (unsigned i = 0; i != NumOps; ++i) {
02879     bool inMem = false;
02880     SDValue Arg = Outs[i].Val;
02881     ISD::ArgFlagsTy Flags = Outs[i].Flags;
02882 
02883     // PtrOff will be used to store the current argument to the stack if a
02884     // register cannot be found for it.
02885     SDValue PtrOff;
02886 
02887     PtrOff = DAG.getConstant(ArgOffset, StackPtr.getValueType());
02888 
02889     PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, PtrOff);
02890 
02891     // On PPC64, promote integers to 64-bit values.
02892     if (isPPC64 && Arg.getValueType() == MVT::i32) {
02893       // FIXME: Should this use ANY_EXTEND if neither sext nor zext?
02894       unsigned ExtOp = Flags.isSExt() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
02895       Arg = DAG.getNode(ExtOp, dl, MVT::i64, Arg);
02896     }
02897 
02898     // FIXME memcpy is used way more than necessary.  Correctness first.
02899     if (Flags.isByVal()) {
02900       unsigned Size = Flags.getByValSize();
02901       if (Size==1 || Size==2) {
02902         // Very small objects are passed right-justified.
02903         // Everything else is passed left-justified.
02904         EVT VT = (Size==1) ? MVT::i8 : MVT::i16;
02905         if (GPR_idx != NumGPRs) {
02906           SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, dl, PtrVT, Chain, Arg,
02907                                           NULL, 0, VT);
02908           MemOpChains.push_back(Load.getValue(1));
02909           RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
02910 
02911           ArgOffset += PtrByteSize;
02912         } else {
02913           SDValue Const = DAG.getConstant(4 - Size, PtrOff.getValueType());
02914           SDValue AddPtr = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, Const);
02915           SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, AddPtr,
02916                                 CallSeqStart.getNode()->getOperand(0),
02917                                 Flags, DAG, dl);
02918           // This must go outside the CALLSEQ_START..END.
02919           SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
02920                                CallSeqStart.getNode()->getOperand(1));
02921           DAG.ReplaceAllUsesWith(CallSeqStart.getNode(),
02922                                  NewCallSeqStart.getNode());
02923           Chain = CallSeqStart = NewCallSeqStart;
02924           ArgOffset += PtrByteSize;
02925         }
02926         continue;
02927       }
02928       // Copy entire object into memory.  There are cases where gcc-generated
02929       // code assumes it is there, even if it could be put entirely into
02930       // registers.  (This is not what the doc says.)
02931       SDValue MemcpyCall = CreateCopyOfByValArgument(Arg, PtrOff,
02932                             CallSeqStart.getNode()->getOperand(0),
02933                             Flags, DAG, dl);
02934       // This must go outside the CALLSEQ_START..END.
02935       SDValue NewCallSeqStart = DAG.getCALLSEQ_START(MemcpyCall,
02936                            CallSeqStart.getNode()->getOperand(1));
02937       DAG.ReplaceAllUsesWith(CallSeqStart.getNode(), NewCallSeqStart.getNode());
02938       Chain = CallSeqStart = NewCallSeqStart;
02939       // And copy the pieces of it that fit into registers.
02940       for (unsigned j=0; j<Size; j+=PtrByteSize) {
02941         SDValue Const = DAG.getConstant(j, PtrOff.getValueType());
02942         SDValue AddArg = DAG.getNode(ISD::ADD, dl, PtrVT, Arg, Const);
02943         if (GPR_idx != NumGPRs) {
02944           SDValue Load = DAG.getLoad(PtrVT, dl, Chain, AddArg, NULL, 0);
02945           MemOpChains.push_back(Load.getValue(1));
02946           RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
02947           ArgOffset += PtrByteSize;
02948         } else {
02949           ArgOffset += ((Size - j + PtrByteSize-1)/PtrByteSize)*PtrByteSize;
02950           break;
02951         }
02952       }
02953       continue;
02954     }
02955 
02956     switch (Arg.getValueType().getSimpleVT().SimpleTy) {
02957     default: llvm_unreachable("Unexpected ValueType for argument!");
02958     case MVT::i32:
02959     case MVT::i64:
02960       if (GPR_idx != NumGPRs) {
02961         RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Arg));
02962       } else {
02963         LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
02964                          isPPC64, isTailCall, false, MemOpChains,
02965                          TailCallArguments, dl);
02966         inMem = true;
02967       }
02968       ArgOffset += PtrByteSize;
02969       break;
02970     case MVT::f32:
02971     case MVT::f64:
02972       if (FPR_idx != NumFPRs) {
02973         RegsToPass.push_back(std::make_pair(FPR[FPR_idx++], Arg));
02974 
02975         if (isVarArg) {
02976           SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
02977           MemOpChains.push_back(Store);
02978 
02979           // Float varargs are always shadowed in available integer registers
02980           if (GPR_idx != NumGPRs) {
02981             SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
02982             MemOpChains.push_back(Load.getValue(1));
02983             RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
02984           }
02985           if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 && !isPPC64){
02986             SDValue ConstFour = DAG.getConstant(4, PtrOff.getValueType());
02987             PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff, ConstFour);
02988             SDValue Load = DAG.getLoad(PtrVT, dl, Store, PtrOff, NULL, 0);
02989             MemOpChains.push_back(Load.getValue(1));
02990             RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
02991           }
02992         } else {
02993           // If we have any FPRs remaining, we may also have GPRs remaining.
02994           // Args passed in FPRs consume either 1 (f32) or 2 (f64) available
02995           // GPRs.
02996           if (GPR_idx != NumGPRs)
02997             ++GPR_idx;
02998           if (GPR_idx != NumGPRs && Arg.getValueType() == MVT::f64 &&
02999               !isPPC64)  // PPC64 has 64-bit GPR's obviously :)
03000             ++GPR_idx;
03001         }
03002       } else {
03003         LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
03004                          isPPC64, isTailCall, false, MemOpChains,
03005                          TailCallArguments, dl);
03006         inMem = true;
03007       }
03008       if (isPPC64)
03009         ArgOffset += 8;
03010       else
03011         ArgOffset += Arg.getValueType() == MVT::f32 ? 4 : 8;
03012       break;
03013     case MVT::v4f32:
03014     case MVT::v4i32:
03015     case MVT::v8i16:
03016     case MVT::v16i8:
03017       if (isVarArg) {
03018         // These go aligned on the stack, or in the corresponding R registers
03019         // when within range.  The Darwin PPC ABI doc claims they also go in
03020         // V registers; in fact gcc does this only for arguments that are
03021         // prototyped, not for those that match the ...  We do it for all
03022         // arguments, seems to work.
03023         while (ArgOffset % 16 !=0) {
03024           ArgOffset += PtrByteSize;
03025           if (GPR_idx != NumGPRs)
03026             GPR_idx++;
03027         }
03028         // We could elide this store in the case where the object fits
03029         // entirely in R registers.  Maybe later.
03030         PtrOff = DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr,
03031                             DAG.getConstant(ArgOffset, PtrVT));
03032         SDValue Store = DAG.getStore(Chain, dl, Arg, PtrOff, NULL, 0);
03033         MemOpChains.push_back(Store);
03034         if (VR_idx != NumVRs) {
03035           SDValue Load = DAG.getLoad(MVT::v4f32, dl, Store, PtrOff, NULL, 0);
03036           MemOpChains.push_back(Load.getValue(1));
03037           RegsToPass.push_back(std::make_pair(VR[VR_idx++], Load));
03038         }
03039         ArgOffset += 16;
03040         for (unsigned i=0; i<16; i+=PtrByteSize) {
03041           if (GPR_idx == NumGPRs)
03042             break;
03043           SDValue Ix = DAG.getNode(ISD::ADD, dl, PtrVT, PtrOff,
03044                                   DAG.getConstant(i, PtrVT));
03045           SDValue Load = DAG.getLoad(PtrVT, dl, Store, Ix, NULL, 0);
03046           MemOpChains.push_back(Load.getValue(1));
03047           RegsToPass.push_back(std::make_pair(GPR[GPR_idx++], Load));
03048         }
03049         break;
03050       }
03051 
03052       // Non-varargs Altivec params generally go in registers, but have
03053       // stack space allocated at the end.
03054       if (VR_idx != NumVRs) {
03055         // Doesn't have GPR space allocated.
03056         RegsToPass.push_back(std::make_pair(VR[VR_idx++], Arg));
03057       } else if (nAltivecParamsAtEnd==0) {
03058         // We are emitting Altivec params in order.
03059         LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
03060                          isPPC64, isTailCall, true, MemOpChains,
03061                          TailCallArguments, dl);
03062         ArgOffset += 16;
03063       }
03064       break;
03065     }
03066   }
03067   // If all Altivec parameters fit in registers, as they usually do,
03068   // they get stack space following the non-Altivec parameters.  We
03069   // don't track this here because nobody below needs it.
03070   // If there are more Altivec parameters than fit in registers emit
03071   // the stores here.
03072   if (!isVarArg && nAltivecParamsAtEnd > NumVRs) {
03073     unsigned j = 0;
03074     // Offset is aligned; skip 1st 12 params which go in V registers.
03075     ArgOffset = ((ArgOffset+15)/16)*16;
03076     ArgOffset += 12*16;
03077     for (unsigned i = 0; i != NumOps; ++i) {
03078       SDValue Arg = Outs[i].Val;
03079       EVT ArgType = Arg.getValueType();
03080       if (ArgType==MVT::v4f32 || ArgType==MVT::v4i32 ||
03081           ArgType==MVT::v8i16 || ArgType==MVT::v16i8) {
03082         if (++j > NumVRs) {
03083           SDValue PtrOff;
03084           // We are emitting Altivec params in order.
03085           LowerMemOpCallTo(DAG, MF, Chain, Arg, PtrOff, SPDiff, ArgOffset,
03086                            isPPC64, isTailCall, true, MemOpChains,
03087                            TailCallArguments, dl);
03088           ArgOffset += 16;
03089         }
03090       }
03091     }
03092   }
03093 
03094   if (!MemOpChains.empty())
03095     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
03096                         &MemOpChains[0], MemOpChains.size());
03097 
03098   // Build a sequence of copy-to-reg nodes chained together with token chain
03099   // and flag operands which copy the outgoing args into the appropriate regs.
03100   SDValue InFlag;
03101   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
03102     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
03103                              RegsToPass[i].second, InFlag);
03104     InFlag = Chain.getValue(1);
03105   }
03106 
03107   if (isTailCall) {
03108     PrepareTailCall(DAG, InFlag, Chain, dl, isPPC64, SPDiff, NumBytes, LROp,
03109                     FPOp, true, TailCallArguments);
03110   }
03111 
03112   return FinishCall(CallConv, dl, isTailCall, isVarArg, DAG,
03113                     RegsToPass, InFlag, Chain, Callee, SPDiff, NumBytes,
03114                     Ins, InVals);
03115 }
03116 
03117 SDValue
03118 PPCTargetLowering::LowerReturn(SDValue Chain,
03119                                unsigned CallConv, bool isVarArg,
03120                                const SmallVectorImpl<ISD::OutputArg> &Outs,
03121                                DebugLoc dl, SelectionDAG &DAG) {
03122 
03123   SmallVector<CCValAssign, 16> RVLocs;
03124   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
03125                  RVLocs, *DAG.getContext());
03126   CCInfo.AnalyzeReturn(Outs, RetCC_PPC);
03127 
03128   // If this is the first return lowered for this function, add the regs to the
03129   // liveout set for the function.
03130   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
03131     for (unsigned i = 0; i != RVLocs.size(); ++i)
03132       DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
03133   }
03134 
03135   SDValue Flag;
03136 
03137   // Copy the result values into the output registers.
03138   for (unsigned i = 0; i != RVLocs.size(); ++i) {
03139     CCValAssign &VA = RVLocs[i];
03140     assert(VA.isRegLoc() && "Can only return in registers!");
03141     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
03142                              Outs[i].Val, Flag);
03143     Flag = Chain.getValue(1);
03144   }
03145 
03146   if (Flag.getNode())
03147     return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
03148   else
03149     return DAG.getNode(PPCISD::RET_FLAG, dl, MVT::Other, Chain);
03150 }
03151 
03152 SDValue PPCTargetLowering::LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG,
03153                                    const PPCSubtarget &Subtarget) {
03154   // When we pop the dynamic allocation we need to restore the SP link.
03155   DebugLoc dl = Op.getDebugLoc();
03156 
03157   // Get the corect type for pointers.
03158   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
03159 
03160   // Construct the stack pointer operand.
03161   bool IsPPC64 = Subtarget.isPPC64();
03162   unsigned SP = IsPPC64 ? PPC::X1 : PPC::R1;
03163   SDValue StackPtr = DAG.getRegister(SP, PtrVT);
03164 
03165   // Get the operands for the STACKRESTORE.
03166   SDValue Chain = Op.getOperand(0);
03167   SDValue SaveSP = Op.getOperand(1);
03168 
03169   // Load the old link SP.
03170   SDValue LoadLinkSP = DAG.getLoad(PtrVT, dl, Chain, StackPtr, NULL, 0);
03171 
03172   // Restore the stack pointer.
03173   Chain = DAG.getCopyToReg(LoadLinkSP.getValue(1), dl, SP, SaveSP);
03174 
03175   // Store the old link SP.
03176   return DAG.getStore(Chain, dl, LoadLinkSP, StackPtr, NULL, 0);
03177 }
03178 
03179 
03180 
03181 SDValue
03182 PPCTargetLowering::getReturnAddrFrameIndex(SelectionDAG & DAG) const {
03183   MachineFunction &MF = DAG.getMachineFunction();
03184   bool IsPPC64 = PPCSubTarget.isPPC64();
03185   bool isDarwinABI = PPCSubTarget.isDarwinABI();
03186   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
03187 
03188   // Get current frame pointer save index.  The users of this index will be
03189   // primarily DYNALLOC instructions.
03190   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
03191   int RASI = FI->getReturnAddrSaveIndex();
03192 
03193   // If the frame pointer save index hasn't been defined yet.
03194   if (!RASI) {
03195     // Find out what the fix offset of the frame pointer save area.
03196     int LROffset = PPCFrameInfo::getReturnSaveOffset(IsPPC64, isDarwinABI);
03197     // Allocate the frame index for frame pointer save area.
03198     RASI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, LROffset);
03199     // Save the result.
03200     FI->setReturnAddrSaveIndex(RASI);
03201   }
03202   return DAG.getFrameIndex(RASI, PtrVT);
03203 }
03204 
03205 SDValue
03206 PPCTargetLowering::getFramePointerFrameIndex(SelectionDAG & DAG) const {
03207   MachineFunction &MF = DAG.getMachineFunction();
03208   bool IsPPC64 = PPCSubTarget.isPPC64();
03209   bool isDarwinABI = PPCSubTarget.isDarwinABI();
03210   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
03211 
03212   // Get current frame pointer save index.  The users of this index will be
03213   // primarily DYNALLOC instructions.
03214   PPCFunctionInfo *FI = MF.getInfo<PPCFunctionInfo>();
03215   int FPSI = FI->getFramePointerSaveIndex();
03216 
03217   // If the frame pointer save index hasn't been defined yet.
03218   if (!FPSI) {
03219     // Find out what the fix offset of the frame pointer save area.
03220     int FPOffset = PPCFrameInfo::getFramePointerSaveOffset(IsPPC64,
03221                                                            isDarwinABI);
03222 
03223     // Allocate the frame index for frame pointer save area.
03224     FPSI = MF.getFrameInfo()->CreateFixedObject(IsPPC64? 8 : 4, FPOffset);
03225     // Save the result.
03226     FI->setFramePointerSaveIndex(FPSI);
03227   }
03228   return DAG.getFrameIndex(FPSI, PtrVT);
03229 }
03230 
03231 SDValue PPCTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
03232                                          SelectionDAG &DAG,
03233                                          const PPCSubtarget &Subtarget) {
03234   // Get the inputs.
03235   SDValue Chain = Op.getOperand(0);
03236   SDValue Size  = Op.getOperand(1);
03237   DebugLoc dl = Op.getDebugLoc();
03238 
03239   // Get the corect type for pointers.
03240   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
03241   // Negate the size.
03242   SDValue NegSize = DAG.getNode(ISD::SUB, dl, PtrVT,
03243                                   DAG.getConstant(0, PtrVT), Size);
03244   // Construct a node for the frame pointer save index.
03245   SDValue FPSIdx = getFramePointerFrameIndex(DAG);
03246   // Build a DYNALLOC node.
03247   SDValue Ops[3] = { Chain, NegSize, FPSIdx };
03248   SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other);
03249   return DAG.getNode(PPCISD::DYNALLOC, dl, VTs, Ops, 3);
03250 }
03251 
03252 /// LowerSELECT_CC - Lower floating point select_cc's into fsel instruction when
03253 /// possible.
03254 SDValue PPCTargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
03255   // Not FP? Not a fsel.
03256   if (!Op.getOperand(0).getValueType().isFloatingPoint() ||
03257       !Op.getOperand(2).getValueType().isFloatingPoint())
03258     return Op;
03259 
03260   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
03261 
03262   // Cannot handle SETEQ/SETNE.
03263   if (CC == ISD::SETEQ || CC == ISD::SETNE) return Op;
03264 
03265   EVT ResVT = Op.getValueType();
03266   EVT CmpVT = Op.getOperand(0).getValueType();
03267   SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
03268   SDValue TV  = Op.getOperand(2), FV  = Op.getOperand(3);
03269   DebugLoc dl = Op.getDebugLoc();
03270 
03271   // If the RHS of the comparison is a 0.0, we don't need to do the
03272   // subtraction at all.
03273   if (isFloatingPointZero(RHS))
03274     switch (CC) {
03275     default: break;       // SETUO etc aren't handled by fsel.
03276     case ISD::SETULT:
03277     case ISD::SETLT:
03278       std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
03279     case ISD::SETOGE:
03280     case ISD::SETGE:
03281       if (LHS.getValueType() == MVT::f32)   // Comparison is always 64-bits
03282         LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
03283       return DAG.getNode(PPCISD::FSEL, dl, ResVT, LHS, TV, FV);
03284     case ISD::SETUGT:
03285     case ISD::SETGT:
03286       std::swap(TV, FV);  // fsel is natively setge, swap operands for setlt
03287     case ISD::SETOLE:
03288     case ISD::SETLE:
03289       if (LHS.getValueType() == MVT::f32)   // Comparison is always 64-bits
03290         LHS = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, LHS);
03291       return DAG.getNode(PPCISD::FSEL, dl, ResVT,
03292                          DAG.getNode(ISD::FNEG, dl, MVT::f64, LHS), TV, FV);
03293     }
03294 
03295   SDValue Cmp;
03296   switch (CC) {
03297   default: break;       // SETUO etc aren't handled by fsel.
03298   case ISD::SETULT:
03299   case ISD::SETLT:
03300     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
03301     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
03302       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
03303       return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
03304   case ISD::SETOGE:
03305   case ISD::SETGE:
03306     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, LHS, RHS);
03307     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
03308       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
03309       return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
03310   case ISD::SETUGT:
03311   case ISD::SETGT:
03312     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
03313     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
03314       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
03315       return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, FV, TV);
03316   case ISD::SETOLE:
03317   case ISD::SETLE:
03318     Cmp = DAG.getNode(ISD::FSUB, dl, CmpVT, RHS, LHS);
03319     if (Cmp.getValueType() == MVT::f32)   // Comparison is always 64-bits
03320       Cmp = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Cmp);
03321       return DAG.getNode(PPCISD::FSEL, dl, ResVT, Cmp, TV, FV);
03322   }
03323   return Op;
03324 }
03325 
03326 // FIXME: Split this code up when LegalizeDAGTypes lands.
03327 SDValue PPCTargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG,
03328                                            DebugLoc dl) {
03329   assert(Op.getOperand(0).getValueType().isFloatingPoint());
03330   SDValue Src = Op.getOperand(0);
03331   if (Src.getValueType() == MVT::f32)
03332     Src = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Src);
03333 
03334   SDValue Tmp;
03335   switch (Op.getValueType().getSimpleVT().SimpleTy) {
03336   default: llvm_unreachable("Unhandled FP_TO_INT type in custom expander!");
03337   case MVT::i32:
03338     Tmp = DAG.getNode(Op.getOpcode()==ISD::FP_TO_SINT ? PPCISD::FCTIWZ :
03339                                                          PPCISD::FCTIDZ, 
03340                       dl, MVT::f64, Src);
03341     break;
03342   case MVT::i64:
03343     Tmp = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Src);
03344     break;
03345   }
03346 
03347   // Convert the FP value to an int value through memory.
03348   SDValue FIPtr = DAG.CreateStackTemporary(MVT::f64);
03349 
03350   // Emit a store to the stack slot.
03351   SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Tmp, FIPtr, NULL, 0);
03352 
03353   // Result is a load from the stack slot.  If loading 4 bytes, make sure to
03354   // add in a bias.
03355   if (Op.getValueType() == MVT::i32)
03356     FIPtr = DAG.getNode(ISD::ADD, dl, FIPtr.getValueType(), FIPtr,
03357                         DAG.getConstant(4, FIPtr.getValueType()));
03358   return DAG.getLoad(Op.getValueType(), dl, Chain, FIPtr, NULL, 0);
03359 }
03360 
03361 SDValue PPCTargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
03362   DebugLoc dl = Op.getDebugLoc();
03363   // Don't handle ppc_fp128 here; let it be lowered to a libcall.
03364   if (Op.getValueType() != MVT::f32 && Op.getValueType() != MVT::f64)
03365     return SDValue();
03366 
03367   if (Op.getOperand(0).getValueType() == MVT::i64) {
03368     SDValue Bits = DAG.getNode(ISD::BIT_CONVERT, dl,
03369                                MVT::f64, Op.getOperand(0));
03370     SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Bits);
03371     if (Op.getValueType() == MVT::f32)
03372       FP = DAG.getNode(ISD::FP_ROUND, dl,
03373                        MVT::f32, FP, DAG.getIntPtrConstant(0));
03374     return FP;
03375   }
03376 
03377   assert(Op.getOperand(0).getValueType() == MVT::i32 &&
03378          "Unhandled SINT_TO_FP type in custom expander!");
03379   // Since we only generate this in 64-bit mode, we can take advantage of
03380   // 64-bit registers.  In particular, sign extend the input value into the
03381   // 64-bit register with extsw, store the WHOLE 64-bit value into the stack
03382   // then lfd it and fcfid it.
03383   MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
03384   int FrameIdx = FrameInfo->CreateStackObject(8, 8);
03385   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
03386   SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
03387 
03388   SDValue Ext64 = DAG.getNode(PPCISD::EXTSW_32, dl, MVT::i32,
03389                                 Op.getOperand(0));
03390 
03391   // STD the extended value into the stack slot.
03392   MachineMemOperand MO(PseudoSourceValue::getFixedStack(FrameIdx),
03393                        MachineMemOperand::MOStore, 0, 8, 8);
03394   SDValue Store = DAG.getNode(PPCISD::STD_32, dl, MVT::Other,
03395                                 DAG.getEntryNode(), Ext64, FIdx,
03396                                 DAG.getMemOperand(MO));
03397   // Load the value as a double.
03398   SDValue Ld = DAG.getLoad(MVT::f64, dl, Store, FIdx, NULL, 0);
03399 
03400   // FCFID it and return it.
03401   SDValue FP = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Ld);
03402   if (Op.getValueType() == MVT::f32)
03403     FP = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, FP, DAG.getIntPtrConstant(0));
03404   return FP;
03405 }
03406 
03407 SDValue PPCTargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
03408   DebugLoc dl = Op.getDebugLoc();
03409   /*
03410    The rounding mode is in bits 30:31 of FPSR, and has the following
03411    settings:
03412      00 Round to nearest
03413      01 Round to 0
03414      10 Round to +inf
03415      11 Round to -inf
03416 
03417   FLT_ROUNDS, on the other hand, expects the following:
03418     -1 Undefined
03419      0 Round to 0
03420      1 Round to nearest
03421      2 Round to +inf
03422      3 Round to -inf
03423 
03424   To perform the conversion, we do:
03425     ((FPSCR & 0x3) ^ ((~FPSCR & 0x3) >> 1))
03426   */
03427 
03428   MachineFunction &MF = DAG.getMachineFunction();
03429   EVT VT = Op.getValueType();
03430   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
03431   std::vector<EVT> NodeTys;
03432   SDValue MFFSreg, InFlag;
03433 
03434   // Save FP Control Word to register
03435   NodeTys.push_back(MVT::f64);    // return register
03436   NodeTys.push_back(MVT::Flag);   // unused in this context
03437   SDValue Chain = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
03438 
03439   // Save FP register to stack slot
03440   int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
03441   SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
03442   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Chain,
03443                                  StackSlot, NULL, 0);
03444 
03445   // Load FP Control Word from low 32 bits of stack slot.
03446   SDValue Four = DAG.getConstant(4, PtrVT);
03447   SDValue Addr = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, Four);
03448   SDValue CWD = DAG.getLoad(MVT::i32, dl, Store, Addr, NULL, 0);
03449 
03450   // Transform as necessary
03451   SDValue CWD1 =
03452     DAG.getNode(ISD::AND, dl, MVT::i32,
03453                 CWD, DAG.getConstant(3, MVT::i32));
03454   SDValue CWD2 =
03455     DAG.getNode(ISD::SRL, dl, MVT::i32,
03456                 DAG.getNode(ISD::AND, dl, MVT::i32,
03457                             DAG.getNode(ISD::XOR, dl, MVT::i32,
03458                                         CWD, DAG.getConstant(3, MVT::i32)),
03459                             DAG.getConstant(3, MVT::i32)),
03460                 DAG.getConstant(1, MVT::i32));
03461 
03462   SDValue RetVal =
03463     DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1, CWD2);
03464 
03465   return DAG.getNode((VT.getSizeInBits() < 16 ?
03466                       ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
03467 }
03468 
03469 SDValue PPCTargetLowering::LowerSHL_PARTS(SDValue Op, SelectionDAG &DAG) {
03470   EVT VT = Op.getValueType();
03471   unsigned BitWidth = VT.getSizeInBits();
03472   DebugLoc dl = Op.getDebugLoc();
03473   assert(Op.getNumOperands() == 3 &&
03474          VT == Op.getOperand(1).getValueType() &&
03475          "Unexpected SHL!");
03476 
03477   // Expand into a bunch of logical ops.  Note that these ops
03478   // depend on the PPC behavior for oversized shift amounts.
03479   SDValue Lo = Op.getOperand(0);
03480   SDValue Hi = Op.getOperand(1);
03481   SDValue Amt = Op.getOperand(2);
03482   EVT AmtVT = Amt.getValueType();
03483 
03484   SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
03485                              DAG.getConstant(BitWidth, AmtVT), Amt);
03486   SDValue Tmp2 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Amt);
03487   SDValue Tmp3 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Tmp1);
03488   SDValue Tmp4 = DAG.getNode(ISD::OR , dl, VT, Tmp2, Tmp3);
03489   SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
03490                              DAG.getConstant(-BitWidth, AmtVT));
03491   SDValue Tmp6 = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Tmp5);
03492   SDValue OutHi = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
03493   SDValue OutLo = DAG.getNode(PPCISD::SHL, dl, VT, Lo, Amt);
03494   SDValue OutOps[] = { OutLo, OutHi };
03495   return DAG.getMergeValues(OutOps, 2, dl);
03496 }
03497 
03498 SDValue PPCTargetLowering::LowerSRL_PARTS(SDValue Op, SelectionDAG &DAG) {
03499   EVT VT = Op.getValueType();
03500   DebugLoc dl = Op.getDebugLoc();
03501   unsigned BitWidth = VT.getSizeInBits();
03502   assert(Op.getNumOperands() == 3 &&
03503          VT == Op.getOperand(1).getValueType() &&
03504          "Unexpected SRL!");
03505 
03506   // Expand into a bunch of logical ops.  Note that these ops
03507   // depend on the PPC behavior for oversized shift amounts.
03508   SDValue Lo = Op.getOperand(0);
03509   SDValue Hi = Op.getOperand(1);
03510   SDValue Amt = Op.getOperand(2);
03511   EVT AmtVT = Amt.getValueType();
03512 
03513   SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
03514                              DAG.getConstant(BitWidth, AmtVT), Amt);
03515   SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
03516   SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
03517   SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
03518   SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
03519                              DAG.getConstant(-BitWidth, AmtVT));
03520   SDValue Tmp6 = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Tmp5);
03521   SDValue OutLo = DAG.getNode(ISD::OR, dl, VT, Tmp4, Tmp6);
03522   SDValue OutHi = DAG.getNode(PPCISD::SRL, dl, VT, Hi, Amt);
03523   SDValue OutOps[] = { OutLo, OutHi };
03524   return DAG.getMergeValues(OutOps, 2, dl);
03525 }
03526 
03527 SDValue PPCTargetLowering::LowerSRA_PARTS(SDValue Op, SelectionDAG &DAG) {
03528   DebugLoc dl = Op.getDebugLoc();
03529   EVT VT = Op.getValueType();
03530   unsigned BitWidth = VT.getSizeInBits();
03531   assert(Op.getNumOperands() == 3 &&
03532          VT == Op.getOperand(1).getValueType() &&
03533          "Unexpected SRA!");
03534 
03535   // Expand into a bunch of logical ops, followed by a select_cc.
03536   SDValue Lo = Op.getOperand(0);
03537   SDValue Hi = Op.getOperand(1);
03538   SDValue Amt = Op.getOperand(2);
03539   EVT AmtVT = Amt.getValueType();
03540 
03541   SDValue Tmp1 = DAG.getNode(ISD::SUB, dl, AmtVT,
03542                              DAG.getConstant(BitWidth, AmtVT), Amt);
03543   SDValue Tmp2 = DAG.getNode(PPCISD::SRL, dl, VT, Lo, Amt);
03544   SDValue Tmp3 = DAG.getNode(PPCISD::SHL, dl, VT, Hi, Tmp1);
03545   SDValue Tmp4 = DAG.getNode(ISD::OR, dl, VT, Tmp2, Tmp3);
03546   SDValue Tmp5 = DAG.getNode(ISD::ADD, dl, AmtVT, Amt,
03547                              DAG.getConstant(-BitWidth, AmtVT));
03548   SDValue Tmp6 = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Tmp5);
03549   SDValue OutHi = DAG.getNode(PPCISD::SRA, dl, VT, Hi, Amt);
03550   SDValue OutLo = DAG.getSelectCC(dl, Tmp5, DAG.getConstant(0, AmtVT),
03551                                   Tmp4, Tmp6, ISD::SETLE);
03552   SDValue OutOps[] = { OutLo, OutHi };
03553   return DAG.getMergeValues(OutOps, 2, dl);
03554 }
03555 
03556 //===----------------------------------------------------------------------===//
03557 // Vector related lowering.
03558 //
03559 
03560 /// BuildSplatI - Build a canonical splati of Val with an element size of
03561 /// SplatSize.  Cast the result to VT.
03562 static SDValue BuildSplatI(int Val, unsigned SplatSize, EVT VT,
03563                              SelectionDAG &DAG, DebugLoc dl) {
03564   assert(Val >= -16 && Val <= 15 && "vsplti is out of range!");
03565 
03566   static const EVT VTys[] = { // canonical VT to use for each size.
03567     MVT::v16i8, MVT::v8i16, MVT::Other, MVT::v4i32
03568   };
03569 
03570   EVT ReqVT = VT != MVT::Other ? VT : VTys[SplatSize-1];
03571 
03572   // Force vspltis[hw] -1 to vspltisb -1 to canonicalize.
03573   if (Val == -1)
03574     SplatSize = 1;
03575 
03576   EVT CanonicalVT = VTys[SplatSize-1];
03577 
03578   // Build a canonical splat for this value.
03579   SDValue Elt = DAG.getConstant(Val, MVT::i32);
03580   SmallVector<SDValue, 8> Ops;
03581   Ops.assign(CanonicalVT.getVectorNumElements(), Elt);
03582   SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, CanonicalVT,
03583                               &Ops[0], Ops.size());
03584   return DAG.getNode(ISD::BIT_CONVERT, dl, ReqVT, Res);
03585 }
03586 
03587 /// BuildIntrinsicOp - Return a binary operator intrinsic node with the
03588 /// specified intrinsic ID.
03589 static SDValue BuildIntrinsicOp(unsigned IID, SDValue LHS, SDValue RHS,
03590                                 SelectionDAG &DAG, DebugLoc dl,
03591                                 EVT DestVT = MVT::Other) {
03592   if (DestVT == MVT::Other) DestVT = LHS.getValueType();
03593   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
03594                      DAG.getConstant(IID, MVT::i32), LHS, RHS);
03595 }
03596 
03597 /// BuildIntrinsicOp - Return a ternary operator intrinsic node with the
03598 /// specified intrinsic ID.
03599 static SDValue BuildIntrinsicOp(unsigned IID, SDValue Op0, SDValue Op1,
03600                                 SDValue Op2, SelectionDAG &DAG,
03601                                 DebugLoc dl, EVT DestVT = MVT::Other) {
03602   if (DestVT == MVT::Other) DestVT = Op0.getValueType();
03603   return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, DestVT,
03604                      DAG.getConstant(IID, MVT::i32), Op0, Op1, Op2);
03605 }
03606 
03607 
03608 /// BuildVSLDOI - Return a VECTOR_SHUFFLE that is a vsldoi of the specified
03609 /// amount.  The result has the specified value type.
03610 static SDValue BuildVSLDOI(SDValue LHS, SDValue RHS, unsigned Amt,
03611                              EVT VT, SelectionDAG &DAG, DebugLoc dl) {
03612   // Force LHS/RHS to be the right type.
03613   LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, LHS);
03614   RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, RHS);
03615 
03616   int Ops[16];
03617   for (unsigned i = 0; i != 16; ++i)
03618     Ops[i] = i + Amt;
03619   SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, LHS, RHS, Ops);
03620   return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
03621 }
03622 
03623 // If this is a case we can't handle, return null and let the default
03624 // expansion code take care of it.  If we CAN select this case, and if it
03625 // selects to a single instruction, return Op.  Otherwise, if we can codegen
03626 // this case more efficiently than a constant pool load, lower it to the
03627 // sequence of ops that should be used.
03628 SDValue PPCTargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
03629   DebugLoc dl = Op.getDebugLoc();
03630   BuildVectorSDNode *BVN = dyn_cast<BuildVectorSDNode>(Op.getNode());
03631   assert(BVN != 0 && "Expected a BuildVectorSDNode in LowerBUILD_VECTOR");
03632 
03633   // Check if this is a splat of a constant value.
03634   APInt APSplatBits, APSplatUndef;
03635   unsigned SplatBitSize;
03636   bool HasAnyUndefs;
03637   if (! BVN->isConstantSplat(APSplatBits, APSplatUndef, SplatBitSize,
03638                              HasAnyUndefs) || SplatBitSize > 32)
03639     return SDValue();
03640 
03641   unsigned SplatBits = APSplatBits.getZExtValue();
03642   unsigned SplatUndef = APSplatUndef.getZExtValue();
03643   unsigned SplatSize = SplatBitSize / 8;
03644 
03645   // First, handle single instruction cases.
03646 
03647   // All zeros?
03648   if (SplatBits == 0) {
03649     // Canonicalize all zero vectors to be v4i32.
03650     if (Op.getValueType() != MVT::v4i32 || HasAnyUndefs) {
03651       SDValue Z = DAG.getConstant(0, MVT::i32);
03652       Z = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Z, Z, Z, Z);
03653       Op = DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Z);
03654     }
03655     return Op;
03656   }
03657 
03658   // If the sign extended value is in the range [-16,15], use VSPLTI[bhw].
03659   int32_t SextVal= (int32_t(SplatBits << (32-SplatBitSize)) >>
03660                     (32-SplatBitSize));
03661   if (SextVal >= -16 && SextVal <= 15)
03662     return BuildSplatI(SextVal, SplatSize, Op.getValueType(), DAG, dl);
03663 
03664 
03665   // Two instruction sequences.
03666 
03667   // If this value is in the range [-32,30] and is even, use:
03668   //    tmp = VSPLTI[bhw], result = add tmp, tmp
03669   if (SextVal >= -32 && SextVal <= 30 && (SextVal & 1) == 0) {
03670     SDValue Res = BuildSplatI(SextVal >> 1, SplatSize, MVT::Other, DAG, dl);
03671     Res = DAG.getNode(ISD::ADD, dl, Res.getValueType(), Res, Res);
03672     return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
03673   }
03674 
03675   // If this is 0x8000_0000 x 4, turn into vspltisw + vslw.  If it is
03676   // 0x7FFF_FFFF x 4, turn it into not(0x8000_0000).  This is important
03677   // for fneg/fabs.
03678   if (SplatSize == 4 && SplatBits == (0x7FFFFFFF&~SplatUndef)) {
03679     // Make -1 and vspltisw -1:
03680     SDValue OnesV = BuildSplatI(-1, 4, MVT::v4i32, DAG, dl);
03681 
03682     // Make the VSLW intrinsic, computing 0x8000_0000.
03683     SDValue Res = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, OnesV,
03684                                    OnesV, DAG, dl);
03685 
03686     // xor by OnesV to invert it.
03687     Res = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Res, OnesV);
03688     return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
03689   }
03690 
03691   // Check to see if this is a wide variety of vsplti*, binop self cases.
03692   static const signed char SplatCsts[] = {
03693     -1, 1, -2, 2, -3, 3, -4, 4, -5, 5, -6, 6, -7, 7,
03694     -8, 8, -9, 9, -10, 10, -11, 11, -12, 12, -13, 13, 14, -14, 15, -15, -16
03695   };
03696 
03697   for (unsigned idx = 0; idx < array_lengthof(SplatCsts); ++idx) {
03698     // Indirect through the SplatCsts array so that we favor 'vsplti -1' for
03699     // cases which are ambiguous (e.g. formation of 0x8000_0000).  'vsplti -1'
03700     int i = SplatCsts[idx];
03701 
03702     // Figure out what shift amount will be used by altivec if shifted by i in
03703     // this splat size.
03704     unsigned TypeShiftAmt = i & (SplatBitSize-1);
03705 
03706     // vsplti + shl self.
03707     if (SextVal == (i << (int)TypeShiftAmt)) {
03708       SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
03709       static const unsigned IIDs[] = { // Intrinsic to use for each size.
03710         Intrinsic::ppc_altivec_vslb, Intrinsic::ppc_altivec_vslh, 0,
03711         Intrinsic::ppc_altivec_vslw
03712       };
03713       Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
03714       return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
03715     }
03716 
03717     // vsplti + srl self.
03718     if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
03719       SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
03720       static const unsigned IIDs[] = { // Intrinsic to use for each size.
03721         Intrinsic::ppc_altivec_vsrb, Intrinsic::ppc_altivec_vsrh, 0,
03722         Intrinsic::ppc_altivec_vsrw
03723       };
03724       Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
03725       return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
03726     }
03727 
03728     // vsplti + sra self.
03729     if (SextVal == (int)((unsigned)i >> TypeShiftAmt)) {
03730       SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
03731       static const unsigned IIDs[] = { // Intrinsic to use for each size.
03732         Intrinsic::ppc_altivec_vsrab, Intrinsic::ppc_altivec_vsrah, 0,
03733         Intrinsic::ppc_altivec_vsraw
03734       };
03735       Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
03736       return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
03737     }
03738 
03739     // vsplti + rol self.
03740     if (SextVal == (int)(((unsigned)i << TypeShiftAmt) |
03741                          ((unsigned)i >> (SplatBitSize-TypeShiftAmt)))) {
03742       SDValue Res = BuildSplatI(i, SplatSize, MVT::Other, DAG, dl);
03743       static const unsigned IIDs[] = { // Intrinsic to use for each size.
03744         Intrinsic::ppc_altivec_vrlb, Intrinsic::ppc_altivec_vrlh, 0,
03745         Intrinsic::ppc_altivec_vrlw
03746       };
03747       Res = BuildIntrinsicOp(IIDs[SplatSize-1], Res, Res, DAG, dl);
03748       return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Res);
03749     }
03750 
03751     // t = vsplti c, result = vsldoi t, t, 1
03752     if (SextVal == ((i << 8) | (i >> (TypeShiftAmt-8)))) {
03753       SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
03754       return BuildVSLDOI(T, T, 1, Op.getValueType(), DAG, dl);
03755     }
03756     // t = vsplti c, result = vsldoi t, t, 2
03757     if (SextVal == ((i << 16) | (i >> (TypeShiftAmt-16)))) {
03758       SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
03759       return BuildVSLDOI(T, T, 2, Op.getValueType(), DAG, dl);
03760     }
03761     // t = vsplti c, result = vsldoi t, t, 3
03762     if (SextVal == ((i << 24) | (i >> (TypeShiftAmt-24)))) {
03763       SDValue T = BuildSplatI(i, SplatSize, MVT::v16i8, DAG, dl);
03764       return BuildVSLDOI(T, T, 3, Op.getValueType(), DAG, dl);
03765     }
03766   }
03767 
03768   // Three instruction sequences.
03769 
03770   // Odd, in range [17,31]:  (vsplti C)-(vsplti -16).
03771   if (SextVal >= 0 && SextVal <= 31) {
03772     SDValue LHS = BuildSplatI(SextVal-16, SplatSize, MVT::Other, DAG, dl);
03773     SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
03774     LHS = DAG.getNode(ISD::SUB, dl, LHS.getValueType(), LHS, RHS);
03775     return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
03776   }
03777   // Odd, in range [-31,-17]:  (vsplti C)+(vsplti -16).
03778   if (SextVal >= -31 && SextVal <= 0) {
03779     SDValue LHS = BuildSplatI(SextVal+16, SplatSize, MVT::Other, DAG, dl);
03780     SDValue RHS = BuildSplatI(-16, SplatSize, MVT::Other, DAG, dl);
03781     LHS = DAG.getNode(ISD::ADD, dl, LHS.getValueType(), LHS, RHS);
03782     return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), LHS);
03783   }
03784 
03785   return SDValue();
03786 }
03787 
03788 /// GeneratePerfectShuffle - Given an entry in the perfect-shuffle table, emit
03789 /// the specified operations to build the shuffle.
03790 static SDValue GeneratePerfectShuffle(unsigned PFEntry, SDValue LHS,
03791                                       SDValue RHS, SelectionDAG &DAG,
03792                                       DebugLoc dl) {
03793   unsigned OpNum = (PFEntry >> 26) & 0x0F;
03794   unsigned LHSID = (PFEntry >> 13) & ((1 << 13)-1);
03795   unsigned RHSID = (PFEntry >>  0) & ((1 << 13)-1);
03796 
03797   enum {
03798     OP_COPY = 0,  // Copy, used for things like <u,u,u,3> to say it is <0,1,2,3>
03799     OP_VMRGHW,
03800     OP_VMRGLW,
03801     OP_VSPLTISW0,
03802     OP_VSPLTISW1,
03803     OP_VSPLTISW2,
03804     OP_VSPLTISW3,
03805     OP_VSLDOI4,
03806     OP_VSLDOI8,
03807     OP_VSLDOI12
03808   };
03809 
03810   if (OpNum == OP_COPY) {
03811     if (LHSID == (1*9+2)*9+3) return LHS;
03812     assert(LHSID == ((4*9+5)*9+6)*9+7 && "Illegal OP_COPY!");
03813     return RHS;
03814   }
03815 
03816   SDValue OpLHS, OpRHS;
03817   OpLHS = GeneratePerfectShuffle(PerfectShuffleTable[LHSID], LHS, RHS, DAG, dl);
03818   OpRHS = GeneratePerfectShuffle(PerfectShuffleTable[RHSID], LHS, RHS, DAG, dl);
03819 
03820   int ShufIdxs[16];
03821   switch (OpNum) {
03822   default: llvm_unreachable("Unknown i32 permute!");
03823   case OP_VMRGHW:
03824     ShufIdxs[ 0] =  0; ShufIdxs[ 1] =  1; ShufIdxs[ 2] =  2; ShufIdxs[ 3] =  3;
03825     ShufIdxs[ 4] = 16; ShufIdxs[ 5] = 17; ShufIdxs[ 6] = 18; ShufIdxs[ 7] = 19;
03826     ShufIdxs[ 8] =  4; ShufIdxs[ 9] =  5; ShufIdxs[10] =  6; ShufIdxs[11] =  7;
03827     ShufIdxs[12] = 20; ShufIdxs[13] = 21; ShufIdxs[14] = 22; ShufIdxs[15] = 23;
03828     break;
03829   case OP_VMRGLW:
03830     ShufIdxs[ 0] =  8; ShufIdxs[ 1] =  9; ShufIdxs[ 2] = 10; ShufIdxs[ 3] = 11;
03831     ShufIdxs[ 4] = 24; ShufIdxs[ 5] = 25; ShufIdxs[ 6] = 26; ShufIdxs[ 7] = 27;
03832     ShufIdxs[ 8] = 12; ShufIdxs[ 9] = 13; ShufIdxs[10] = 14; ShufIdxs[11] = 15;
03833     ShufIdxs[12] = 28; ShufIdxs[13] = 29; ShufIdxs[14] = 30; ShufIdxs[15] = 31;
03834     break;
03835   case OP_VSPLTISW0:
03836     for (unsigned i = 0; i != 16; ++i)
03837       ShufIdxs[i] = (i&3)+0;
03838     break;
03839   case OP_VSPLTISW1:
03840     for (unsigned i = 0; i != 16; ++i)
03841       ShufIdxs[i] = (i&3)+4;
03842     break;
03843   case OP_VSPLTISW2:
03844     for (unsigned i = 0; i != 16; ++i)
03845       ShufIdxs[i] = (i&3)+8;
03846     break;
03847   case OP_VSPLTISW3:
03848     for (unsigned i = 0; i != 16; ++i)
03849       ShufIdxs[i] = (i&3)+12;
03850     break;
03851   case OP_VSLDOI4:
03852     return BuildVSLDOI(OpLHS, OpRHS, 4, OpLHS.getValueType(), DAG, dl);
03853   case OP_VSLDOI8:
03854     return BuildVSLDOI(OpLHS, OpRHS, 8, OpLHS.getValueType(), DAG, dl);
03855   case OP_VSLDOI12:
03856     return BuildVSLDOI(OpLHS, OpRHS, 12, OpLHS.getValueType(), DAG, dl);
03857   }
03858   EVT VT = OpLHS.getValueType();
03859   OpLHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpLHS);
03860   OpRHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OpRHS);
03861   SDValue T = DAG.getVectorShuffle(MVT::v16i8, dl, OpLHS, OpRHS, ShufIdxs);
03862   return DAG.getNode(ISD::BIT_CONVERT, dl, VT, T);
03863 }
03864 
03865 /// LowerVECTOR_SHUFFLE - Return the code we lower for VECTOR_SHUFFLE.  If this
03866 /// is a shuffle we can handle in a single instruction, return it.  Otherwise,
03867 /// return the code it can be lowered into.  Worst case, it can always be
03868 /// lowered into a vperm.
03869 SDValue PPCTargetLowering::LowerVECTOR_SHUFFLE(SDValue Op,
03870                                                SelectionDAG &DAG) {
03871   DebugLoc dl = Op.getDebugLoc();
03872   SDValue V1 = Op.getOperand(0);
03873   SDValue V2 = Op.getOperand(1);
03874   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
03875   EVT VT = Op.getValueType();
03876 
03877   // Cases that are handled by instructions that take permute immediates
03878   // (such as vsplt*) should be left as VECTOR_SHUFFLE nodes so they can be
03879   // selected by the instruction selector.
03880   if (V2.getOpcode() == ISD::UNDEF) {
03881     if (PPC::isSplatShuffleMask(SVOp, 1) ||
03882         PPC::isSplatShuffleMask(SVOp, 2) ||
03883         PPC::isSplatShuffleMask(SVOp, 4) ||
03884         PPC::isVPKUWUMShuffleMask(SVOp, true) ||
03885         PPC::isVPKUHUMShuffleMask(SVOp, true) ||
03886         PPC::isVSLDOIShuffleMask(SVOp, true) != -1 ||
03887         PPC::isVMRGLShuffleMask(SVOp, 1, true) ||
03888         PPC::isVMRGLShuffleMask(SVOp, 2, true) ||
03889         PPC::isVMRGLShuffleMask(SVOp, 4, true) ||
03890         PPC::isVMRGHShuffleMask(SVOp, 1, true) ||
03891         PPC::isVMRGHShuffleMask(SVOp, 2, true) ||
03892         PPC::isVMRGHShuffleMask(SVOp, 4, true)) {
03893       return Op;
03894     }
03895   }
03896 
03897   // Altivec has a variety of "shuffle immediates" that take two vector inputs
03898   // and produce a fixed permutation.  If any of these match, do not lower to
03899   // VPERM.
03900   if (PPC::isVPKUWUMShuffleMask(SVOp, false) ||
03901       PPC::isVPKUHUMShuffleMask(SVOp, false) ||
03902       PPC::isVSLDOIShuffleMask(SVOp, false) != -1 ||
03903       PPC::isVMRGLShuffleMask(SVOp, 1, false) ||
03904       PPC::isVMRGLShuffleMask(SVOp, 2, false) ||
03905       PPC::isVMRGLShuffleMask(SVOp, 4, false) ||
03906       PPC::isVMRGHShuffleMask(SVOp, 1, false) ||
03907       PPC::isVMRGHShuffleMask(SVOp, 2, false) ||
03908       PPC::isVMRGHShuffleMask(SVOp, 4, false))
03909     return Op;
03910 
03911   // Check to see if this is a shuffle of 4-byte values.  If so, we can use our
03912   // perfect shuffle table to emit an optimal matching sequence.
03913   SmallVector<int, 16> PermMask;
03914   SVOp->getMask(PermMask);
03915   
03916   unsigned PFIndexes[4];
03917   bool isFourElementShuffle = true;
03918   for (unsigned i = 0; i != 4 && isFourElementShuffle; ++i) { // Element number
03919     unsigned EltNo = 8;   // Start out undef.
03920     for (unsigned j = 0; j != 4; ++j) {  // Intra-element byte.
03921       if (PermMask[i*4+j] < 0)
03922         continue;   // Undef, ignore it.
03923 
03924       unsigned ByteSource = PermMask[i*4+j];
03925       if ((ByteSource & 3) != j) {
03926         isFourElementShuffle = false;
03927         break;
03928       }
03929 
03930       if (EltNo == 8) {
03931         EltNo = ByteSource/4;
03932       } else if (EltNo != ByteSource/4) {
03933         isFourElementShuffle = false;
03934         break;
03935       }
03936     }
03937     PFIndexes[i] = EltNo;
03938   }
03939 
03940   // If this shuffle can be expressed as a shuffle of 4-byte elements, use the
03941   // perfect shuffle vector to determine if it is cost effective to do this as
03942   // discrete instructions, or whether we should use a vperm.
03943   if (isFourElementShuffle) {
03944     // Compute the index in the perfect shuffle table.
03945     unsigned PFTableIndex =
03946       PFIndexes[0]*9*9*9+PFIndexes[1]*9*9+PFIndexes[2]*9+PFIndexes[3];
03947 
03948     unsigned PFEntry = PerfectShuffleTable[PFTableIndex];
03949     unsigned Cost  = (PFEntry >> 30);
03950 
03951     // Determining when to avoid vperm is tricky.  Many things affect the cost
03952     // of vperm, particularly how many times the perm mask needs to be computed.
03953     // For example, if the perm mask can be hoisted out of a loop or is already
03954     // used (perhaps because there are multiple permutes with the same shuffle
03955     // mask?) the vperm has a cost of 1.  OTOH, hoisting the permute mask out of
03956     // the loop requires an extra register.
03957     //
03958     // As a compromise, we only emit discrete instructions if the shuffle can be
03959     // generated in 3 or fewer operations.  When we have loop information
03960     // available, if this block is within a loop, we should avoid using vperm
03961     // for 3-operation perms and use a constant pool load instead.
03962     if (Cost < 3)
03963       return GeneratePerfectShuffle(PFEntry, V1, V2, DAG, dl);
03964   }
03965 
03966   // Lower this to a VPERM(V1, V2, V3) expression, where V3 is a constant
03967   // vector that will get spilled to the constant pool.
03968   if (V2.getOpcode() == ISD::UNDEF) V2 = V1;
03969 
03970   // The SHUFFLE_VECTOR mask is almost exactly what we want for vperm, except
03971   // that it is in input element units, not in bytes.  Convert now.
03972   EVT EltVT = V1.getValueType().getVectorElementType();
03973   unsigned BytesPerElement = EltVT.getSizeInBits()/8;
03974 
03975   SmallVector<SDValue, 16> ResultMask;
03976   for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) {
03977     unsigned SrcElt = PermMask[i] < 0 ? 0 : PermMask[i];
03978 
03979     for (unsigned j = 0; j != BytesPerElement; ++j)
03980       ResultMask.push_back(DAG.getConstant(SrcElt*BytesPerElement+j,
03981                                            MVT::i32));
03982   }
03983 
03984   SDValue VPermMask = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i8,
03985                                     &ResultMask[0], ResultMask.size());
03986   return DAG.getNode(PPCISD::VPERM, dl, V1.getValueType(), V1, V2, VPermMask);
03987 }
03988 
03989 /// getAltivecCompareInfo - Given an intrinsic, return false if it is not an
03990 /// altivec comparison.  If it is, return true and fill in Opc/isDot with
03991 /// information about the intrinsic.
03992 static bool getAltivecCompareInfo(SDValue Intrin, int &CompareOpc,
03993                                   bool &isDot) {
03994   unsigned IntrinsicID =
03995     cast<ConstantSDNode>(Intrin.getOperand(0))->getZExtValue();
03996   CompareOpc = -1;
03997   isDot = false;
03998   switch (IntrinsicID) {
03999   default: return false;
04000     // Comparison predicates.
04001   case Intrinsic::ppc_altivec_vcmpbfp_p:  CompareOpc = 966; isDot = 1; break;
04002   case Intrinsic::ppc_altivec_vcmpeqfp_p: CompareOpc = 198; isDot = 1; break;
04003   case Intrinsic::ppc_altivec_vcmpequb_p: CompareOpc =   6; isDot = 1; break;
04004   case Intrinsic::ppc_altivec_vcmpequh_p: CompareOpc =  70; isDot = 1; break;
04005   case Intrinsic::ppc_altivec_vcmpequw_p: CompareOpc = 134; isDot = 1; break;
04006   case Intrinsic::ppc_altivec_vcmpgefp_p: CompareOpc = 454; isDot = 1; break;
04007   case Intrinsic::ppc_altivec_vcmpgtfp_p: CompareOpc = 710; isDot = 1; break;
04008   case Intrinsic::ppc_altivec_vcmpgtsb_p: CompareOpc = 774; isDot = 1; break;
04009   case Intrinsic::ppc_altivec_vcmpgtsh_p: CompareOpc = 838; isDot = 1; break;
04010   case Intrinsic::ppc_altivec_vcmpgtsw_p: CompareOpc = 902; isDot = 1; break;
04011   case Intrinsic::ppc_altivec_vcmpgtub_p: CompareOpc = 518; isDot = 1; break;
04012   case Intrinsic::ppc_altivec_vcmpgtuh_p: CompareOpc = 582; isDot = 1; break;
04013   case Intrinsic::ppc_altivec_vcmpgtuw_p: CompareOpc = 646; isDot = 1; break;
04014 
04015     // Normal Comparisons.
04016   case Intrinsic::ppc_altivec_vcmpbfp:    CompareOpc = 966; isDot = 0; break;
04017   case Intrinsic::ppc_altivec_vcmpeqfp:   CompareOpc = 198; isDot = 0; break;
04018   case Intrinsic::ppc_altivec_vcmpequb:   CompareOpc =   6; isDot = 0; break;
04019   case Intrinsic::ppc_altivec_vcmpequh:   CompareOpc =  70; isDot = 0; break;
04020   case Intrinsic::ppc_altivec_vcmpequw:   CompareOpc = 134; isDot = 0; break;
04021   case Intrinsic::ppc_altivec_vcmpgefp:   CompareOpc = 454; isDot = 0; break;
04022   case Intrinsic::ppc_altivec_vcmpgtfp:   CompareOpc = 710; isDot = 0; break;
04023   case Intrinsic::ppc_altivec_vcmpgtsb:   CompareOpc = 774; isDot = 0; break;
04024   case Intrinsic::ppc_altivec_vcmpgtsh:   CompareOpc = 838; isDot = 0; break;
04025   case Intrinsic::ppc_altivec_vcmpgtsw:   CompareOpc = 902; isDot = 0; break;
04026   case Intrinsic::ppc_altivec_vcmpgtub:   CompareOpc = 518; isDot = 0; break;
04027   case Intrinsic::ppc_altivec_vcmpgtuh:   CompareOpc = 582; isDot = 0; break;
04028   case Intrinsic::ppc_altivec_vcmpgtuw:   CompareOpc = 646; isDot = 0; break;
04029   }
04030   return true;
04031 }
04032 
04033 /// LowerINTRINSIC_WO_CHAIN - If this is an intrinsic that we want to custom
04034 /// lower, do it, otherwise return null.
04035 SDValue PPCTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
04036                                                      SelectionDAG &DAG) {
04037   // If this is a lowered altivec predicate compare, CompareOpc is set to the
04038   // opcode number of the comparison.
04039   DebugLoc dl = Op.getDebugLoc();
04040   int CompareOpc;
04041   bool isDot;
04042   if (!getAltivecCompareInfo(Op, CompareOpc, isDot))
04043     return SDValue();    // Don't custom lower most intrinsics.
04044 
04045   // If this is a non-dot comparison, make the VCMP node and we are done.
04046   if (!isDot) {
04047     SDValue Tmp = DAG.getNode(PPCISD::VCMP, dl, Op.getOperand(2).getValueType(),
04048                                 Op.getOperand(1), Op.getOperand(2),
04049                                 DAG.getConstant(CompareOpc, MVT::i32));
04050     return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Tmp);
04051   }
04052 
04053   // Create the PPCISD altivec 'dot' comparison node.
04054   SDValue Ops[] = {
04055     Op.getOperand(2),  // LHS
04056     Op.getOperand(3),  // RHS
04057     DAG.getConstant(CompareOpc, MVT::i32)
04058   };
04059   std::vector<EVT> VTs;
04060   VTs.push_back(Op.getOperand(2).getValueType());
04061   VTs.push_back(MVT::Flag);
04062   SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
04063 
04064   // Now that we have the comparison, emit a copy from the CR to a GPR.
04065   // This is flagged to the above dot comparison.
04066   SDValue Flags = DAG.getNode(PPCISD::MFCR, dl, MVT::i32,
04067                                 DAG.getRegister(PPC::CR6, MVT::i32),
04068                                 CompNode.getValue(1));
04069 
04070   // Unpack the result based on how the target uses it.
04071   unsigned BitNo;   // Bit # of CR6.
04072   bool InvertBit;   // Invert result?
04073   switch (cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue()) {
04074   default:  // Can't happen, don't crash on invalid number though.
04075   case 0:   // Return the value of the EQ bit of CR6.
04076     BitNo = 0; InvertBit = false;
04077     break;
04078   case 1:   // Return the inverted value of the EQ bit of CR6.
04079     BitNo = 0; InvertBit = true;
04080     break;
04081   case 2:   // Return the value of the LT bit of CR6.
04082     BitNo = 2; InvertBit = false;
04083     break;
04084   case 3:   // Return the inverted value of the LT bit of CR6.
04085     BitNo = 2; InvertBit = true;
04086     break;
04087   }
04088 
04089   // Shift the bit into the low position.
04090   Flags = DAG.getNode(ISD::SRL, dl, MVT::i32, Flags,
04091                       DAG.getConstant(8-(3-BitNo), MVT::i32));
04092   // Isolate the bit.
04093   Flags = DAG.getNode(ISD::AND, dl, MVT::i32, Flags,
04094                       DAG.getConstant(1, MVT::i32));
04095 
04096   // If we are supposed to, toggle the bit.
04097   if (InvertBit)
04098     Flags = DAG.getNode(ISD::XOR, dl, MVT::i32, Flags,
04099                         DAG.getConstant(1, MVT::i32));
04100   return Flags;
04101 }
04102 
04103 SDValue PPCTargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op,
04104                                                    SelectionDAG &DAG) {
04105   DebugLoc dl = Op.getDebugLoc();
04106   // Create a stack slot that is 16-byte aligned.
04107   MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
04108   int FrameIdx = FrameInfo->CreateStackObject(16, 16);
04109   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
04110   SDValue FIdx = DAG.getFrameIndex(FrameIdx, PtrVT);
04111 
04112   // Store the input value into Value#0 of the stack slot.
04113   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
04114                                  Op.getOperand(0), FIdx, NULL, 0);
04115   // Load it out.
04116   return DAG.getLoad(Op.getValueType(), dl, Store, FIdx, NULL, 0);
04117 }
04118 
04119 SDValue PPCTargetLowering::LowerMUL(SDValue Op, SelectionDAG &DAG) {
04120   DebugLoc dl = Op.getDebugLoc();
04121   if (Op.getValueType() == MVT::v4i32) {
04122     SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
04123 
04124     SDValue Zero  = BuildSplatI(  0, 1, MVT::v4i32, DAG, dl);
04125     SDValue Neg16 = BuildSplatI(-16, 4, MVT::v4i32, DAG, dl);//+16 as shift amt.
04126 
04127     SDValue RHSSwap =   // = vrlw RHS, 16
04128       BuildIntrinsicOp(Intrinsic::ppc_altivec_vrlw, RHS, Neg16, DAG, dl);
04129 
04130     // Shrinkify inputs to v8i16.
04131     LHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, LHS);
04132     RHS = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHS);
04133     RHSSwap = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, RHSSwap);
04134 
04135     // Low parts multiplied together, generating 32-bit results (we ignore the
04136     // top parts).
04137     SDValue LoProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmulouh,
04138                                         LHS, RHS, DAG, dl, MVT::v4i32);
04139 
04140     SDValue HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmsumuhm,
04141                                       LHS, RHSSwap, Zero, DAG, dl, MVT::v4i32);
04142     // Shift the high parts up 16 bits.
04143     HiProd = BuildIntrinsicOp(Intrinsic::ppc_altivec_vslw, HiProd,
04144                               Neg16, DAG, dl);
04145     return DAG.getNode(ISD::ADD, dl, MVT::v4i32, LoProd, HiProd);
04146   } else if (Op.getValueType() == MVT::v8i16) {
04147     SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
04148 
04149     SDValue Zero = BuildSplatI(0, 1, MVT::v8i16, DAG, dl);
04150 
04151     return BuildIntrinsicOp(Intrinsic::ppc_altivec_vmladduhm,
04152                             LHS, RHS, Zero, DAG, dl);
04153   } else if (Op.getValueType() == MVT::v16i8) {
04154     SDValue LHS = Op.getOperand(0), RHS = Op.getOperand(1);
04155 
04156     // Multiply the even 8-bit parts, producing 16-bit sums.
04157     SDValue EvenParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuleub,
04158                                            LHS, RHS, DAG, dl, MVT::v8i16);
04159     EvenParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, EvenParts);
04160 
04161     // Multiply the odd 8-bit parts, producing 16-bit sums.
04162     SDValue OddParts = BuildIntrinsicOp(Intrinsic::ppc_altivec_vmuloub,
04163                                           LHS, RHS, DAG, dl, MVT::v8i16);
04164     OddParts = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, OddParts);
04165 
04166     // Merge the results together.
04167     int Ops[16];
04168     for (unsigned i = 0; i != 8; ++i) {
04169       Ops[i*2  ] = 2*i+1;
04170       Ops[i*2+1] = 2*i+1+16;
04171     }
04172     return DAG.getVectorShuffle(MVT::v16i8, dl, EvenParts, OddParts, Ops);
04173   } else {
04174     llvm_unreachable("Unknown mul to lower!");
04175   }
04176 }
04177 
04178 /// LowerOperation - Provide custom lowering hooks for some operations.
04179 ///
04180 SDValue PPCTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
04181   switch (Op.getOpcode()) {
04182   default: llvm_unreachable("Wasn't expecting to be able to lower this!");
04183   case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
04184   case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
04185   case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
04186   case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
04187   case ISD::SETCC:              return LowerSETCC(Op, DAG);
04188   case ISD::TRAMPOLINE:         return LowerTRAMPOLINE(Op, DAG);
04189   case ISD::VASTART:
04190     return LowerVASTART(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
04191                         VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
04192 
04193   case ISD::VAARG:
04194     return LowerVAARG(Op, DAG, VarArgsFrameIndex, VarArgsStackOffset,
04195                       VarArgsNumGPR, VarArgsNumFPR, PPCSubTarget);
04196 
04197   case ISD::STACKRESTORE:       return LowerSTACKRESTORE(Op, DAG, PPCSubTarget);
04198   case ISD::DYNAMIC_STACKALLOC:
04199     return LowerDYNAMIC_STACKALLOC(Op, DAG, PPCSubTarget);
04200 
04201   case ISD::SELECT_CC:          return LowerSELECT_CC(Op, DAG);
04202   case ISD::FP_TO_UINT:
04203   case ISD::FP_TO_SINT:         return LowerFP_TO_INT(Op, DAG,
04204                                                        Op.getDebugLoc());
04205   case ISD::SINT_TO_FP:         return LowerSINT_TO_FP(Op, DAG);
04206   case ISD::FLT_ROUNDS_:        return LowerFLT_ROUNDS_(Op, DAG);
04207 
04208   // Lower 64-bit shifts.
04209   case ISD::SHL_PARTS:          return LowerSHL_PARTS(Op, DAG);
04210   case ISD::SRL_PARTS:          return LowerSRL_PARTS(Op, DAG);
04211   case ISD::SRA_PARTS:          return LowerSRA_PARTS(Op, DAG);
04212 
04213   // Vector-related lowering.
04214   case ISD::BUILD_VECTOR:       return LowerBUILD_VECTOR(Op, DAG);
04215   case ISD::VECTOR_SHUFFLE:     return LowerVECTOR_SHUFFLE(Op, DAG);
04216   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
04217   case ISD::SCALAR_TO_VECTOR:   return LowerSCALAR_TO_VECTOR(Op, DAG);
04218   case ISD::MUL:                return LowerMUL(Op, DAG);
04219 
04220   // Frame & Return address.
04221   case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
04222   case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
04223   }
04224   return SDValue();
04225 }
04226 
04227 void PPCTargetLowering::ReplaceNodeResults(SDNode *N,
04228                                            SmallVectorImpl<SDValue>&Results,
04229                                            SelectionDAG &DAG) {
04230   DebugLoc dl = N->getDebugLoc();
04231   switch (N->getOpcode()) {
04232   default:
04233     assert(false && "Do not know how to custom type legalize this operation!");
04234     return;
04235   case ISD::FP_ROUND_INREG: {
04236     assert(N->getValueType(0) == MVT::ppcf128);
04237     assert(N->getOperand(0).getValueType() == MVT::ppcf128);
04238     SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
04239                              MVT::f64, N->getOperand(0),
04240                              DAG.getIntPtrConstant(0));
04241     SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
04242                              MVT::f64, N->getOperand(0),
04243                              DAG.getIntPtrConstant(1));
04244 
04245     // This sequence changes FPSCR to do round-to-zero, adds the two halves
04246     // of the long double, and puts FPSCR back the way it was.  We do not
04247     // actually model FPSCR.
04248     std::vector<EVT> NodeTys;
04249     SDValue Ops[4], Result, MFFSreg, InFlag, FPreg;
04250 
04251     NodeTys.push_back(MVT::f64);   // Return register
04252     NodeTys.push_back(MVT::Flag);    // Returns a flag for later insns
04253     Result = DAG.getNode(PPCISD::MFFS, dl, NodeTys, &InFlag, 0);
04254     MFFSreg = Result.getValue(0);
04255     InFlag = Result.getValue(1);
04256 
04257     NodeTys.clear();
04258     NodeTys.push_back(MVT::Flag);   // Returns a flag
04259     Ops[0] = DAG.getConstant(31, MVT::i32);
04260     Ops[1] = InFlag;
04261     Result = DAG.getNode(PPCISD::MTFSB1, dl, NodeTys, Ops, 2);
04262     InFlag = Result.getValue(0);
04263 
04264     NodeTys.clear();
04265     NodeTys.push_back(MVT::Flag);   // Returns a flag
04266     Ops[0] = DAG.getConstant(30, MVT::i32);
04267     Ops[1] = InFlag;
04268     Result = DAG.getNode(PPCISD::MTFSB0, dl, NodeTys, Ops, 2);
04269     InFlag = Result.getValue(0);
04270 
04271     NodeTys.clear();
04272     NodeTys.push_back(MVT::f64);    // result of add
04273     NodeTys.push_back(MVT::Flag);   // Returns a flag
04274     Ops[0] = Lo;
04275     Ops[1] = Hi;
04276     Ops[2] = InFlag;
04277     Result = DAG.getNode(PPCISD::FADDRTZ, dl, NodeTys, Ops, 3);
04278     FPreg = Result.getValue(0);
04279     InFlag = Result.getValue(1);
04280 
04281     NodeTys.clear();
04282     NodeTys.push_back(MVT::f64);
04283     Ops[0] = DAG.getConstant(1, MVT::i32);
04284     Ops[1] = MFFSreg;
04285     Ops[2] = FPreg;
04286     Ops[3] = InFlag;
04287     Result = DAG.getNode(PPCISD::MTFSF, dl, NodeTys, Ops, 4);
04288     FPreg = Result.getValue(0);
04289 
04290     // We know the low half is about to be thrown away, so just use something
04291     // convenient.
04292     Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::ppcf128,
04293                                 FPreg, FPreg));
04294     return;
04295   }
04296   case ISD::FP_TO_SINT:
04297     Results.push_back(LowerFP_TO_INT(SDValue(N, 0), DAG, dl));
04298     return;
04299   }
04300 }
04301 
04302 
04303 //===----------------------------------------------------------------------===//
04304 //  Other Lowering Code
04305 //===----------------------------------------------------------------------===//
04306 
04307 MachineBasicBlock *
04308 PPCTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
04309                                     bool is64bit, unsigned BinOpcode) const {
04310   // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
04311   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
04312 
04313   const BasicBlock *LLVM_BB = BB->getBasicBlock();
04314   MachineFunction *F = BB->getParent();
04315   MachineFunction::iterator It = BB;
04316   ++It;
04317 
04318   unsigned dest = MI->getOperand(0).getReg();
04319   unsigned ptrA = MI->getOperand(1).getReg();
04320   unsigned ptrB = MI->getOperand(2).getReg();
04321   unsigned incr = MI->getOperand(3).getReg();
04322   DebugLoc dl = MI->getDebugLoc();
04323 
04324   MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
04325   MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
04326   F->insert(It, loopMBB);
04327   F->insert(It, exitMBB);
04328   exitMBB->transferSuccessors(BB);
04329 
04330   MachineRegisterInfo &RegInfo = F->getRegInfo();
04331   unsigned TmpReg = (!BinOpcode) ? incr :
04332     RegInfo.createVirtualRegister(
04333        is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
04334                  (const TargetRegisterClass *) &PPC::GPRCRegClass);
04335 
04336   //  thisMBB:
04337   //   ...
04338   //   fallthrough --> loopMBB
04339   BB->addSuccessor(loopMBB);
04340 
04341   //  loopMBB:
04342   //   l[wd]arx dest, ptr
04343   //   add r0, dest, incr
04344   //   st[wd]cx. r0, ptr
04345   //   bne- loopMBB
04346   //   fallthrough --> exitMBB
04347   BB = loopMBB;
04348   BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
04349     .addReg(ptrA).addReg(ptrB);
04350   if (BinOpcode)
04351     BuildMI(BB, dl, TII->get(BinOpcode), TmpReg).addReg(incr).addReg(dest);
04352   BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
04353     .addReg(TmpReg).addReg(ptrA).addReg(ptrB);
04354   BuildMI(BB, dl, TII->get(PPC::BCC))
04355     .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
04356   BB->addSuccessor(loopMBB);
04357   BB->addSuccessor(exitMBB);
04358 
04359   //  exitMBB:
04360   //   ...
04361   BB = exitMBB;
04362   return BB;
04363 }
04364 
04365 MachineBasicBlock *
04366 PPCTargetLowering::EmitPartwordAtomicBinary(MachineInstr *MI,
04367                                             MachineBasicBlock *BB,
04368                                             bool is8bit,    // operation
04369                                             unsigned BinOpcode) const {
04370   // This also handles ATOMIC_SWAP, indicated by BinOpcode==0.
04371   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
04372   // In 64 bit mode we have to use 64 bits for addresses, even though the
04373   // lwarx/stwcx are 32 bits.  With the 32-bit atomics we can use address
04374   // registers without caring whether they're 32 or 64, but here we're
04375   // doing actual arithmetic on the addresses.
04376   bool is64bit = PPCSubTarget.isPPC64();
04377 
04378   const BasicBlock *LLVM_BB = BB->getBasicBlock();
04379   MachineFunction *F = BB->getParent();
04380   MachineFunction::iterator It = BB;
04381   ++It;
04382 
04383   unsigned dest = MI->getOperand(0).getReg();
04384   unsigned ptrA = MI->getOperand(1).getReg();
04385   unsigned ptrB = MI->getOperand(2).getReg();
04386   unsigned incr = MI->getOperand(3).getReg();
04387   DebugLoc dl = MI->getDebugLoc();
04388 
04389   MachineBasicBlock *loopMBB = F->CreateMachineBasicBlock(LLVM_BB);
04390   MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
04391   F->insert(It, loopMBB);
04392   F->insert(It, exitMBB);
04393   exitMBB->transferSuccessors(BB);
04394 
04395   MachineRegisterInfo &RegInfo = F->getRegInfo();
04396   const TargetRegisterClass *RC =
04397     is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
04398               (const TargetRegisterClass *) &PPC::GPRCRegClass;
04399   unsigned PtrReg = RegInfo.createVirtualRegister(RC);
04400   unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
04401   unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
04402   unsigned Incr2Reg = RegInfo.createVirtualRegister(RC);
04403   unsigned MaskReg = RegInfo.createVirtualRegister(RC);
04404   unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
04405   unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
04406   unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
04407   unsigned Tmp3Reg = RegInfo.createVirtualRegister(RC);
04408   unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
04409   unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
04410   unsigned Ptr1Reg;
04411   unsigned TmpReg = (!BinOpcode) ? Incr2Reg : RegInfo.createVirtualRegister(RC);
04412 
04413   //  thisMBB:
04414   //   ...
04415   //   fallthrough --> loopMBB
04416   BB->addSuccessor(loopMBB);
04417 
04418   // The 4-byte load must be aligned, while a char or short may be
04419   // anywhere in the word.  Hence all this nasty bookkeeping code.
04420   //   add ptr1, ptrA, ptrB [copy if ptrA==0]
04421   //   rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
04422   //   xori shift, shift1, 24 [16]
04423   //   rlwinm ptr, ptr1, 0, 0, 29
04424   //   slw incr2, incr, shift
04425   //   li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
04426   //   slw mask, mask2, shift
04427   //  loopMBB:
04428   //   lwarx tmpDest, ptr
04429   //   add tmp, tmpDest, incr2
04430   //   andc tmp2, tmpDest, mask
04431   //   and tmp3, tmp, mask
04432   //   or tmp4, tmp3, tmp2
04433   //   stwcx. tmp4, ptr
04434   //   bne- loopMBB
04435   //   fallthrough --> exitMBB
04436   //   srw dest, tmpDest, shift
04437 
04438   if (ptrA!=PPC::R0) {
04439     Ptr1Reg = RegInfo.createVirtualRegister(RC);
04440     BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
04441       .addReg(ptrA).addReg(ptrB);
04442   } else {
04443     Ptr1Reg = ptrB;
04444   }
04445   BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
04446       .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
04447   BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
04448       .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
04449   if (is64bit)
04450     BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
04451       .addReg(Ptr1Reg).addImm(0).addImm(61);
04452   else
04453     BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
04454       .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
04455   BuildMI(BB, dl, TII->get(PPC::SLW), Incr2Reg)
04456       .addReg(incr).addReg(ShiftReg);
04457   if (is8bit)
04458     BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
04459   else {
04460     BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
04461     BuildMI(BB, dl, TII->get(PPC::ORI),Mask2Reg).addReg(Mask3Reg).addImm(65535);
04462   }
04463   BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
04464       .addReg(Mask2Reg).addReg(ShiftReg);
04465 
04466   BB = loopMBB;
04467   BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
04468     .addReg(PPC::R0).addReg(PtrReg);
04469   if (BinOpcode)
04470     BuildMI(BB, dl, TII->get(BinOpcode), TmpReg)
04471       .addReg(Incr2Reg).addReg(TmpDestReg);
04472   BuildMI(BB, dl, TII->get(is64bit ? PPC::ANDC8 : PPC::ANDC), Tmp2Reg)
04473     .addReg(TmpDestReg).addReg(MaskReg);
04474   BuildMI(BB, dl, TII->get(is64bit ? PPC::AND8 : PPC::AND), Tmp3Reg)
04475     .addReg(TmpReg).addReg(MaskReg);
04476   BuildMI(BB, dl, TII->get(is64bit ? PPC::OR8 : PPC::OR), Tmp4Reg)
04477     .addReg(Tmp3Reg).addReg(Tmp2Reg);
04478   BuildMI(BB, dl, TII->get(PPC::STWCX))
04479     .addReg(Tmp4Reg).addReg(PPC::R0).addReg(PtrReg);
04480   BuildMI(BB, dl, TII->get(PPC::BCC))
04481     .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loopMBB);
04482   BB->addSuccessor(loopMBB);
04483   BB->addSuccessor(exitMBB);
04484 
04485   //  exitMBB:
04486   //   ...
04487   BB = exitMBB;
04488   BuildMI(BB, dl, TII->get(PPC::SRW), dest).addReg(TmpDestReg).addReg(ShiftReg);
04489   return BB;
04490 }
04491 
04492 MachineBasicBlock *
04493 PPCTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
04494                                                MachineBasicBlock *BB) const {
04495   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
04496 
04497   // To "insert" these instructions we actually have to insert their
04498   // control-flow patterns.
04499   const BasicBlock *LLVM_BB = BB->getBasicBlock();
04500   MachineFunction::iterator It = BB;
04501   ++It;
04502 
04503   MachineFunction *F = BB->getParent();
04504 
04505   if (MI->getOpcode() == PPC::SELECT_CC_I4 ||
04506       MI->getOpcode() == PPC::SELECT_CC_I8 ||
04507       MI->getOpcode() == PPC::SELECT_CC_F4 ||
04508       MI->getOpcode() == PPC::SELECT_CC_F8 ||
04509       MI->getOpcode() == PPC::SELECT_CC_VRRC) {
04510 
04511     // The incoming instruction knows the destination vreg to set, the
04512     // condition code register to branch on, the true/false values to
04513     // select between, and a branch opcode to use.
04514 
04515     //  thisMBB:
04516     //  ...
04517     //   TrueVal = ...
04518     //   cmpTY ccX, r1, r2
04519     //   bCC copy1MBB
04520     //   fallthrough --> copy0MBB
04521     MachineBasicBlock *thisMBB = BB;
04522     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
04523     MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
04524     unsigned SelectPred = MI->getOperand(4).getImm();
04525     DebugLoc dl = MI->getDebugLoc();
04526     BuildMI(BB, dl, TII->get(PPC::BCC))
04527       .addImm(SelectPred).addReg(MI->getOperand(1).getReg()).addMBB(sinkMBB);
04528     F->insert(It, copy0MBB);
04529     F->insert(It, sinkMBB);
04530     // Update machine-CFG edges by transferring all successors of the current
04531     // block to the new block which will contain the Phi node for the select.
04532     sinkMBB->transferSuccessors(BB);
04533     // Next, add the true and fallthrough blocks as its successors.
04534     BB->addSuccessor(copy0MBB);
04535     BB->addSuccessor(sinkMBB);
04536 
04537     //  copy0MBB:
04538     //   %FalseValue = ...
04539     //   # fallthrough to sinkMBB
04540     BB = copy0MBB;
04541 
04542     // Update machine-CFG edges
04543     BB->addSuccessor(sinkMBB);
04544 
04545     //  sinkMBB:
04546     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
04547     //  ...
04548     BB = sinkMBB;
04549     BuildMI(BB, dl, TII->get(PPC::PHI), MI->getOperand(0).getReg())
04550       .addReg(MI->getOperand(3).getReg()).addMBB(copy0MBB)
04551       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
04552   }
04553   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I8)
04554     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ADD4);
04555   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I16)
04556     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ADD4);
04557   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I32)
04558     BB = EmitAtomicBinary(MI, BB, false, PPC::ADD4);
04559   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_ADD_I64)
04560     BB = EmitAtomicBinary(MI, BB, true, PPC::ADD8);
04561 
04562   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I8)
04563     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::AND);
04564   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I16)
04565     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::AND);
04566   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I32)
04567     BB = EmitAtomicBinary(MI, BB, false, PPC::AND);
04568   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_AND_I64)
04569     BB = EmitAtomicBinary(MI, BB, true, PPC::AND8);
04570 
04571   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I8)
04572     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::OR);
04573   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I16)
04574     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::OR);
04575   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I32)
04576     BB = EmitAtomicBinary(MI, BB, false, PPC::OR);
04577   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_OR_I64)
04578     BB = EmitAtomicBinary(MI, BB, true, PPC::OR8);
04579 
04580   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I8)
04581     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::XOR);
04582   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I16)
04583     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::XOR);
04584   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I32)
04585     BB = EmitAtomicBinary(MI, BB, false, PPC::XOR);
04586   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_XOR_I64)
04587     BB = EmitAtomicBinary(MI, BB, true, PPC::XOR8);
04588 
04589   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I8)
04590     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::ANDC);
04591   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I16)
04592     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::ANDC);
04593   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I32)
04594     BB = EmitAtomicBinary(MI, BB, false, PPC::ANDC);
04595   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_NAND_I64)
04596     BB = EmitAtomicBinary(MI, BB, true, PPC::ANDC8);
04597 
04598   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I8)
04599     BB = EmitPartwordAtomicBinary(MI, BB, true, PPC::SUBF);
04600   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I16)
04601     BB = EmitPartwordAtomicBinary(MI, BB, false, PPC::SUBF);
04602   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I32)
04603     BB = EmitAtomicBinary(MI, BB, false, PPC::SUBF);
04604   else if (MI->getOpcode() == PPC::ATOMIC_LOAD_SUB_I64)
04605     BB = EmitAtomicBinary(MI, BB, true, PPC::SUBF8);
04606 
04607   else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I8)
04608     BB = EmitPartwordAtomicBinary(MI, BB, true, 0);
04609   else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I16)
04610     BB = EmitPartwordAtomicBinary(MI, BB, false, 0);
04611   else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I32)
04612     BB = EmitAtomicBinary(MI, BB, false, 0);
04613   else if (MI->getOpcode() == PPC::ATOMIC_SWAP_I64)
04614     BB = EmitAtomicBinary(MI, BB, true, 0);
04615 
04616   else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I32 ||
04617            MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64) {
04618     bool is64bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I64;
04619 
04620     unsigned dest   = MI->getOperand(0).getReg();
04621     unsigned ptrA   = MI->getOperand(1).getReg();
04622     unsigned ptrB   = MI->getOperand(2).getReg();
04623     unsigned oldval = MI->getOperand(3).getReg();
04624     unsigned newval = MI->getOperand(4).getReg();
04625     DebugLoc dl     = MI->getDebugLoc();
04626 
04627     MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
04628     MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
04629     MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
04630     MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
04631     F->insert(It, loop1MBB);
04632     F->insert(It, loop2MBB);
04633     F->insert(It, midMBB);
04634     F->insert(It, exitMBB);
04635     exitMBB->transferSuccessors(BB);
04636 
04637     //  thisMBB:
04638     //   ...
04639     //   fallthrough --> loopMBB
04640     BB->addSuccessor(loop1MBB);
04641 
04642     // loop1MBB:
04643     //   l[wd]arx dest, ptr
04644     //   cmp[wd] dest, oldval
04645     //   bne- midMBB
04646     // loop2MBB:
04647     //   st[wd]cx. newval, ptr
04648     //   bne- loopMBB
04649     //   b exitBB
04650     // midMBB:
04651     //   st[wd]cx. dest, ptr
04652     // exitBB:
04653     BB = loop1MBB;
04654     BuildMI(BB, dl, TII->get(is64bit ? PPC::LDARX : PPC::LWARX), dest)
04655       .addReg(ptrA).addReg(ptrB);
04656     BuildMI(BB, dl, TII->get(is64bit ? PPC::CMPD : PPC::CMPW), PPC::CR0)
04657       .addReg(oldval).addReg(dest);
04658     BuildMI(BB, dl, TII->get(PPC::BCC))
04659       .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
04660     BB->addSuccessor(loop2MBB);
04661     BB->addSuccessor(midMBB);
04662 
04663     BB = loop2MBB;
04664     BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
04665       .addReg(newval).addReg(ptrA).addReg(ptrB);
04666     BuildMI(BB, dl, TII->get(PPC::BCC))
04667       .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
04668     BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
04669     BB->addSuccessor(loop1MBB);
04670     BB->addSuccessor(exitMBB);
04671 
04672     BB = midMBB;
04673     BuildMI(BB, dl, TII->get(is64bit ? PPC::STDCX : PPC::STWCX))
04674       .addReg(dest).addReg(ptrA).addReg(ptrB);
04675     BB->addSuccessor(exitMBB);
04676 
04677     //  exitMBB:
04678     //   ...
04679     BB = exitMBB;
04680   } else if (MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8 ||
04681              MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I16) {
04682     // We must use 64-bit registers for addresses when targeting 64-bit,
04683     // since we're actually doing arithmetic on them.  Other registers
04684     // can be 32-bit.
04685     bool is64bit = PPCSubTarget.isPPC64();
04686     bool is8bit = MI->getOpcode() == PPC::ATOMIC_CMP_SWAP_I8;
04687 
04688     unsigned dest   = MI->getOperand(0).getReg();
04689     unsigned ptrA   = MI->getOperand(1).getReg();
04690     unsigned ptrB   = MI->getOperand(2).getReg();
04691     unsigned oldval = MI->getOperand(3).getReg();
04692     unsigned newval = MI->getOperand(4).getReg();
04693     DebugLoc dl     = MI->getDebugLoc();
04694 
04695     MachineBasicBlock *loop1MBB = F->CreateMachineBasicBlock(LLVM_BB);
04696     MachineBasicBlock *loop2MBB = F->CreateMachineBasicBlock(LLVM_BB);
04697     MachineBasicBlock *midMBB = F->CreateMachineBasicBlock(LLVM_BB);
04698     MachineBasicBlock *exitMBB = F->CreateMachineBasicBlock(LLVM_BB);
04699     F->insert(It, loop1MBB);
04700     F->insert(It, loop2MBB);
04701     F->insert(It, midMBB);
04702     F->insert(It, exitMBB);
04703     exitMBB->transferSuccessors(BB);
04704 
04705     MachineRegisterInfo &RegInfo = F->getRegInfo();
04706     const TargetRegisterClass *RC =
04707       is64bit ? (const TargetRegisterClass *) &PPC::G8RCRegClass :
04708                 (const TargetRegisterClass *) &PPC::GPRCRegClass;
04709     unsigned PtrReg = RegInfo.createVirtualRegister(RC);
04710     unsigned Shift1Reg = RegInfo.createVirtualRegister(RC);
04711     unsigned ShiftReg = RegInfo.createVirtualRegister(RC);
04712     unsigned NewVal2Reg = RegInfo.createVirtualRegister(RC);
04713     unsigned NewVal3Reg = RegInfo.createVirtualRegister(RC);
04714     unsigned OldVal2Reg = RegInfo.createVirtualRegister(RC);
04715     unsigned OldVal3Reg = RegInfo.createVirtualRegister(RC);
04716     unsigned MaskReg = RegInfo.createVirtualRegister(RC);
04717     unsigned Mask2Reg = RegInfo.createVirtualRegister(RC);
04718     unsigned Mask3Reg = RegInfo.createVirtualRegister(RC);
04719     unsigned Tmp2Reg = RegInfo.createVirtualRegister(RC);
04720     unsigned Tmp4Reg = RegInfo.createVirtualRegister(RC);
04721     unsigned TmpDestReg = RegInfo.createVirtualRegister(RC);
04722     unsigned Ptr1Reg;
04723     unsigned TmpReg = RegInfo.createVirtualRegister(RC);
04724     //  thisMBB:
04725     //   ...
04726     //   fallthrough --> loopMBB
04727     BB->addSuccessor(loop1MBB);
04728 
04729     // The 4-byte load must be aligned, while a char or short may be
04730     // anywhere in the word.  Hence all this nasty bookkeeping code.
04731     //   add ptr1, ptrA, ptrB [copy if ptrA==0]
04732     //   rlwinm shift1, ptr1, 3, 27, 28 [3, 27, 27]
04733     //   xori shift, shift1, 24 [16]
04734     //   rlwinm ptr, ptr1, 0, 0, 29
04735     //   slw newval2, newval, shift
04736     //   slw oldval2, oldval,shift
04737     //   li mask2, 255 [li mask3, 0; ori mask2, mask3, 65535]
04738     //   slw mask, mask2, shift
04739     //   and newval3, newval2, mask
04740     //   and oldval3, oldval2, mask
04741     // loop1MBB:
04742     //   lwarx tmpDest, ptr
04743     //   and tmp, tmpDest, mask
04744     //   cmpw tmp, oldval3
04745     //   bne- midMBB
04746     // loop2MBB:
04747     //   andc tmp2, tmpDest, mask
04748     //   or tmp4, tmp2, newval3
04749     //   stwcx. tmp4, ptr
04750     //   bne- loop1MBB
04751     //   b exitBB
04752     // midMBB:
04753     //   stwcx. tmpDest, ptr
04754     // exitBB:
04755     //   srw dest, tmpDest, shift
04756     if (ptrA!=PPC::R0) {
04757       Ptr1Reg = RegInfo.createVirtualRegister(RC);
04758       BuildMI(BB, dl, TII->get(is64bit ? PPC::ADD8 : PPC::ADD4), Ptr1Reg)
04759         .addReg(ptrA).addReg(ptrB);
04760     } else {
04761       Ptr1Reg = ptrB;
04762     }
04763     BuildMI(BB, dl, TII->get(PPC::RLWINM), Shift1Reg).addReg(Ptr1Reg)
04764         .addImm(3).addImm(27).addImm(is8bit ? 28 : 27);
04765     BuildMI(BB, dl, TII->get(is64bit ? PPC::XORI8 : PPC::XORI), ShiftReg)
04766         .addReg(Shift1Reg).addImm(is8bit ? 24 : 16);
04767     if (is64bit)
04768       BuildMI(BB, dl, TII->get(PPC::RLDICR), PtrReg)
04769         .addReg(Ptr1Reg).addImm(0).addImm(61);
04770     else
04771       BuildMI(BB, dl, TII->get(PPC::RLWINM), PtrReg)
04772         .addReg(Ptr1Reg).addImm(0).addImm(0).addImm(29);
04773     BuildMI(BB, dl, TII->get(PPC::SLW), NewVal2Reg)
04774         .addReg(newval).addReg(ShiftReg);
04775     BuildMI(BB, dl, TII->get(PPC::SLW), OldVal2Reg)
04776         .addReg(oldval).addReg(ShiftReg);
04777     if (is8bit)
04778       BuildMI(BB, dl, TII->get(PPC::LI), Mask2Reg).addImm(255);
04779     else {
04780       BuildMI(BB, dl, TII->get(PPC::LI), Mask3Reg).addImm(0);
04781       BuildMI(BB, dl, TII->get(PPC::ORI), Mask2Reg)
04782         .addReg(Mask3Reg).addImm(65535);
04783     }
04784     BuildMI(BB, dl, TII->get(PPC::SLW), MaskReg)
04785         .addReg(Mask2Reg).addReg(ShiftReg);
04786     BuildMI(BB, dl, TII->get(PPC::AND), NewVal3Reg)
04787         .addReg(NewVal2Reg).addReg(MaskReg);
04788     BuildMI(BB, dl, TII->get(PPC::AND), OldVal3Reg)
04789         .addReg(OldVal2Reg).addReg(MaskReg);
04790 
04791     BB = loop1MBB;
04792     BuildMI(BB, dl, TII->get(PPC::LWARX), TmpDestReg)
04793         .addReg(PPC::R0).addReg(PtrReg);
04794     BuildMI(BB, dl, TII->get(PPC::AND),TmpReg)
04795         .addReg(TmpDestReg).addReg(MaskReg);
04796     BuildMI(BB, dl, TII->get(PPC::CMPW), PPC::CR0)
04797         .addReg(TmpReg).addReg(OldVal3Reg);
04798     BuildMI(BB, dl, TII->get(PPC::BCC))
04799         .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(midMBB);
04800     BB->addSuccessor(loop2MBB);
04801     BB->addSuccessor(midMBB);
04802 
04803     BB = loop2MBB;
04804     BuildMI(BB, dl, TII->get(PPC::ANDC),Tmp2Reg)
04805         .addReg(TmpDestReg).addReg(MaskReg);
04806     BuildMI(BB, dl, TII->get(PPC::OR),Tmp4Reg)
04807         .addReg(Tmp2Reg).addReg(NewVal3Reg);
04808     BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(Tmp4Reg)
04809         .addReg(PPC::R0).addReg(PtrReg);
04810     BuildMI(BB, dl, TII->get(PPC::BCC))
04811       .addImm(PPC::PRED_NE).addReg(PPC::CR0).addMBB(loop1MBB);
04812     BuildMI(BB, dl, TII->get(PPC::B)).addMBB(exitMBB);
04813     BB->addSuccessor(loop1MBB);
04814     BB->addSuccessor(exitMBB);
04815 
04816     BB = midMBB;
04817     BuildMI(BB, dl, TII->get(PPC::STWCX)).addReg(TmpDestReg)
04818       .addReg(PPC::R0).addReg(PtrReg);
04819     BB->addSuccessor(exitMBB);
04820 
04821     //  exitMBB:
04822     //   ...
04823     BB = exitMBB;
04824     BuildMI(BB, dl, TII->get(PPC::SRW),dest).addReg(TmpReg).addReg(ShiftReg);
04825   } else {
04826     llvm_unreachable("Unexpected instr type to insert");
04827   }
04828 
04829   F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
04830   return BB;
04831 }
04832 
04833 //===----------------------------------------------------------------------===//
04834 // Target Optimization Hooks
04835 //===----------------------------------------------------------------------===//
04836 
04837 SDValue PPCTargetLowering::PerformDAGCombine(SDNode *N,
04838                                              DAGCombinerInfo &DCI) const {
04839   TargetMachine &TM = getTargetMachine();
04840   SelectionDAG &DAG = DCI.DAG;
04841   DebugLoc dl = N->getDebugLoc();
04842   switch (N->getOpcode()) {
04843   default: break;
04844   case PPCISD::SHL:
04845     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
04846       if (C->getZExtValue() == 0)   // 0 << V -> 0.
04847         return N->getOperand(0);
04848     }
04849     break;
04850   case PPCISD::SRL:
04851     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
04852       if (C->getZExtValue() == 0)   // 0 >>u V -> 0.
04853         return N->getOperand(0);
04854     }
04855     break;
04856   case PPCISD::SRA:
04857     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
04858       if (C->getZExtValue() == 0 ||   //  0 >>s V -> 0.
04859           C->isAllOnesValue())    // -1 >>s V -> -1.
04860         return N->getOperand(0);
04861     }
04862     break;
04863 
04864   case ISD::SINT_TO_FP:
04865     if (TM.getSubtarget<PPCSubtarget>().has64BitSupport()) {
04866       if (N->getOperand(0).getOpcode() == ISD::FP_TO_SINT) {
04867         // Turn (sint_to_fp (fp_to_sint X)) -> fctidz/fcfid without load/stores.
04868         // We allow the src/dst to be either f32/f64, but the intermediate
04869         // type must be i64.
04870         if (N->getOperand(0).getValueType() == MVT::i64 &&
04871             N->getOperand(0).getOperand(0).getValueType() != MVT::ppcf128) {
04872           SDValue Val = N->getOperand(0).getOperand(0);
04873           if (Val.getValueType() == MVT::f32) {
04874             Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
04875             DCI.AddToWorklist(Val.getNode());
04876           }
04877 
04878           Val = DAG.getNode(PPCISD::FCTIDZ, dl, MVT::f64, Val);
04879           DCI.AddToWorklist(Val.getNode());
04880           Val = DAG.getNode(PPCISD::FCFID, dl, MVT::f64, Val);
04881           DCI.AddToWorklist(Val.getNode());
04882           if (N->getValueType(0) == MVT::f32) {
04883             Val = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Val,
04884                               DAG.getIntPtrConstant(0));
04885             DCI.AddToWorklist(Val.getNode());
04886           }
04887           return Val;
04888         } else if (N->getOperand(0).getValueType() == MVT::i32) {
04889           // If the intermediate type is i32, we can avoid the load/store here
04890           // too.
04891         }
04892       }
04893     }
04894     break;
04895   case ISD::STORE:
04896     // Turn STORE (FP_TO_SINT F) -> STFIWX(FCTIWZ(F)).
04897     if (TM.getSubtarget<PPCSubtarget>().hasSTFIWX() &&
04898         !cast<StoreSDNode>(N)->isTruncatingStore() &&
04899         N->getOperand(1).getOpcode() == ISD::FP_TO_SINT &&
04900         N->getOperand(1).getValueType() == MVT::i32 &&
04901         N->getOperand(1).getOperand(0).getValueType() != MVT::ppcf128) {
04902       SDValue Val = N->getOperand(1).getOperand(0);
04903       if (Val.getValueType() == MVT::f32) {
04904         Val = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f64, Val);
04905         DCI.AddToWorklist(Val.getNode());
04906       }
04907       Val = DAG.getNode(PPCISD::FCTIWZ, dl, MVT::f64, Val);
04908       DCI.AddToWorklist(Val.getNode());
04909 
04910       Val = DAG.getNode(PPCISD::STFIWX, dl, MVT::Other, N->getOperand(0), Val,
04911                         N->getOperand(2), N->getOperand(3));
04912       DCI.AddToWorklist(Val.getNode());
04913       return Val;
04914     }
04915 
04916     // Turn STORE (BSWAP) -> sthbrx/stwbrx.
04917     if (N->getOperand(1).getOpcode() == ISD::BSWAP &&
04918         N->getOperand(1).getNode()->hasOneUse() &&
04919         (N->getOperand(1).getValueType() == MVT::i32 ||
04920          N->getOperand(1).getValueType() == MVT::i16)) {
04921       SDValue BSwapOp = N->getOperand(1).getOperand(0);
04922       // Do an any-extend to 32-bits if this is a half-word input.
04923       if (BSwapOp.getValueType() == MVT::i16)
04924         BSwapOp = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, BSwapOp);
04925 
04926       return DAG.getNode(PPCISD::STBRX, dl, MVT::Other, N->getOperand(0),
04927                          BSwapOp, N->getOperand(2), N->getOperand(3),
04928                          DAG.getValueType(N->getOperand(1).getValueType()));
04929     }
04930     break;
04931   case ISD::BSWAP:
04932     // Turn BSWAP (LOAD) -> lhbrx/lwbrx.
04933     if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
04934         N->getOperand(0).hasOneUse() &&
04935         (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i16)) {
04936       SDValue Load = N->getOperand(0);
04937       LoadSDNode *LD = cast<LoadSDNode>(Load);
04938       // Create the byte-swapping load.
04939       std::vector<EVT> VTs;
04940       VTs.push_back(MVT::i32);
04941       VTs.push_back(MVT::Other);
04942       SDValue MO = DAG.getMemOperand(LD->getMemOperand());
04943       SDValue Ops[] = {
04944         LD->getChain(),    // Chain
04945         LD->getBasePtr(),  // Ptr
04946         MO,                // MemOperand
04947         DAG.getValueType(N->getValueType(0)) // VT
04948       };
04949       SDValue BSLoad = DAG.getNode(PPCISD::LBRX, dl, VTs, Ops, 4);
04950 
04951       // If this is an i16 load, insert the truncate.
04952       SDValue ResVal = BSLoad;
04953       if (N->getValueType(0) == MVT::i16)
04954         ResVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, BSLoad);
04955 
04956       // First, combine the bswap away.  This makes the value produced by the
04957       // load dead.
04958       DCI.CombineTo(N, ResVal);
04959 
04960       // Next, combine the load away, we give it a bogus result value but a real
04961       // chain result.  The result value is dead because the bswap is dead.
04962       DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
04963 
04964       // Return N so it doesn't get rechecked!
04965       return SDValue(N, 0);
04966     }
04967 
04968     break;
04969   case PPCISD::VCMP: {
04970     // If a VCMPo node already exists with exactly the same operands as this
04971     // node, use its result instead of this node (VCMPo computes both a CR6 and
04972     // a normal output).
04973     //
04974     if (!N->getOperand(0).hasOneUse() &&
04975         !N->getOperand(1).hasOneUse() &&
04976         !N->getOperand(2).hasOneUse()) {
04977 
04978       // Scan all of the users of the LHS, looking for VCMPo's that match.
04979       SDNode *VCMPoNode = 0;
04980 
04981       SDNode *LHSN = N->getOperand(0).getNode();
04982       for (SDNode::use_iterator UI = LHSN->use_begin(), E = LHSN->use_end();
04983            UI != E; ++UI)
04984         if (UI->getOpcode() == PPCISD::VCMPo &&
04985             UI->getOperand(1) == N->getOperand(1) &&
04986             UI->getOperand(2) == N->getOperand(2) &&
04987             UI->getOperand(0) == N->getOperand(0)) {
04988           VCMPoNode = *UI;
04989           break;
04990         }
04991 
04992       // If there is no VCMPo node, or if the flag value has a single use, don't
04993       // transform this.
04994       if (!VCMPoNode || VCMPoNode->hasNUsesOfValue(0, 1))
04995         break;
04996 
04997       // Look at the (necessarily single) use of the flag value.  If it has a
04998       // chain, this transformation is more complex.  Note that multiple things
04999       // could use the value result, which we should ignore.
05000       SDNode *FlagUser = 0;
05001       for (SDNode::use_iterator UI = VCMPoNode->use_begin();
05002            FlagUser == 0; ++UI) {
05003         assert(UI != VCMPoNode->use_end() && "Didn't find user!");
05004         SDNode *User = *UI;
05005         for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
05006           if (User->getOperand(i) == SDValue(VCMPoNode, 1)) {
05007             FlagUser = User;
05008             break;
05009           }
05010         }
05011       }
05012 
05013       // If the user is a MFCR instruction, we know this is safe.  Otherwise we
05014       // give up for right now.
05015       if (FlagUser->getOpcode() == PPCISD::MFCR)
05016         return SDValue(VCMPoNode, 0);
05017     }
05018     break;
05019   }
05020   case ISD::BR_CC: {
05021     // If this is a branch on an altivec predicate comparison, lower this so
05022     // that we don't have to do a MFCR: instead, branch directly on CR6.  This
05023     // lowering is done pre-legalize, because the legalizer lowers the predicate
05024     // compare down to code that is difficult to reassemble.
05025     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
05026     SDValue LHS = N->getOperand(2), RHS = N->getOperand(3);
05027     int CompareOpc;
05028     bool isDot;
05029 
05030     if (LHS.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
05031         isa<ConstantSDNode>(RHS) && (CC == ISD::SETEQ || CC == ISD::SETNE) &&
05032         getAltivecCompareInfo(LHS, CompareOpc, isDot)) {
05033       assert(isDot && "Can't compare against a vector result!");
05034 
05035       // If this is a comparison against something other than 0/1, then we know
05036       // that the condition is never/always true.
05037       unsigned Val = cast<ConstantSDNode>(RHS)->getZExtValue();
05038       if (Val != 0 && Val != 1) {
05039         if (CC == ISD::SETEQ)      // Cond never true, remove branch.
05040           return N->getOperand(0);
05041         // Always !=, turn it into an unconditional branch.
05042         return DAG.getNode(ISD::BR, dl, MVT::Other,
05043                            N->getOperand(0), N->getOperand(4));
05044       }
05045 
05046       bool BranchOnWhenPredTrue = (CC == ISD::SETEQ) ^ (Val == 0);
05047 
05048       // Create the PPCISD altivec 'dot' comparison node.
05049       std::vector<EVT> VTs;
05050       SDValue Ops[] = {
05051         LHS.getOperand(2),  // LHS of compare
05052         LHS.getOperand(3),  // RHS of compare
05053         DAG.getConstant(CompareOpc, MVT::i32)
05054       };
05055       VTs.push_back(LHS.getOperand(2).getValueType());
05056       VTs.push_back(MVT::Flag);
05057       SDValue CompNode = DAG.getNode(PPCISD::VCMPo, dl, VTs, Ops, 3);
05058 
05059       // Unpack the result based on how the target uses it.
05060       PPC::Predicate CompOpc;
05061       switch (cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue()) {
05062       default:  // Can't happen, don't crash on invalid number though.
05063       case 0:   // Branch on the value of the EQ bit of CR6.
05064         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_EQ : PPC::PRED_NE;
05065         break;
05066       case 1:   // Branch on the inverted value of the EQ bit of CR6.
05067         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_NE : PPC::PRED_EQ;
05068         break;
05069       case 2:   // Branch on the value of the LT bit of CR6.
05070         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_LT : PPC::PRED_GE;
05071         break;
05072       case 3:   // Branch on the inverted value of the LT bit of CR6.
05073         CompOpc = BranchOnWhenPredTrue ? PPC::PRED_GE : PPC::PRED_LT;
05074         break;
05075       }
05076 
05077       return DAG.getNode(PPCISD::COND_BRANCH, dl, MVT::Other, N->getOperand(0),
05078                          DAG.getConstant(CompOpc, MVT::i32),
05079                          DAG.getRegister(PPC::CR6, MVT::i32),
05080                          N->getOperand(4), CompNode.getValue(1));
05081     }
05082     break;
05083   }
05084   }
05085 
05086   return SDValue();
05087 }
05088 
05089 //===----------------------------------------------------------------------===//
05090 // Inline Assembly Support
05091 //===----------------------------------------------------------------------===//
05092 
05093 void PPCTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
05094                                                        const APInt &Mask,
05095                                                        APInt &KnownZero,
05096                                                        APInt &KnownOne,
05097                                                        const SelectionDAG &DAG,
05098                                                        unsigned Depth) const {
05099   KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);
05100   switch (Op.getOpcode()) {
05101   default: break;
05102   case PPCISD::LBRX: {
05103     // lhbrx is known to have the top bits cleared out.
05104     if (cast<VTSDNode>(Op.getOperand(3))->getVT() == MVT::i16)
05105       KnownZero = 0xFFFF0000;
05106     break;
05107   }
05108   case ISD::INTRINSIC_WO_CHAIN: {
05109     switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
05110     default: break;
05111     case Intrinsic::ppc_altivec_vcmpbfp_p:
05112     case Intrinsic::ppc_altivec_vcmpeqfp_p:
05113     case Intrinsic::ppc_altivec_vcmpequb_p:
05114     case Intrinsic::ppc_altivec_vcmpequh_p:
05115     case Intrinsic::ppc_altivec_vcmpequw_p:
05116     case Intrinsic::ppc_altivec_vcmpgefp_p:
05117     case Intrinsic::ppc_altivec_vcmpgtfp_p:
05118     case Intrinsic::ppc_altivec_vcmpgtsb_p:
05119     case Intrinsic::ppc_altivec_vcmpgtsh_p:
05120     case Intrinsic::ppc_altivec_vcmpgtsw_p:
05121     case Intrinsic::ppc_altivec_vcmpgtub_p:
05122     case Intrinsic::ppc_altivec_vcmpgtuh_p:
05123     case Intrinsic::ppc_altivec_vcmpgtuw_p:
05124       KnownZero = ~1U;  // All bits but the low one are known to be zero.
05125       break;
05126     }
05127   }
05128   }
05129 }
05130 
05131 
05132 /// getConstraintType - Given a constraint, return the type of
05133 /// constraint it is for this target.
05134 PPCTargetLowering::ConstraintType
05135 PPCTargetLowering::getConstraintType(const std::string &Constraint) const {
05136   if (Constraint.size() == 1) {
05137     switch (Constraint[0]) {
05138     default: break;
05139     case 'b':
05140     case 'r':
05141     case 'f':
05142     case 'v':
05143     case 'y':
05144       return C_RegisterClass;
05145     }
05146   }
05147   return TargetLowering::getConstraintType(Constraint);
05148 }
05149 
05150 std::pair<unsigned, const TargetRegisterClass*>
05151 PPCTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
05152                                                 EVT VT) const {
05153   if (Constraint.size() == 1) {
05154     // GCC RS6000 Constraint Letters
05155     switch (Constraint[0]) {
05156     case 'b':   // R1-R31
05157     case 'r':   // R0-R31
05158       if (VT == MVT::i64 && PPCSubTarget.isPPC64())
05159         return std::make_pair(0U, PPC::G8RCRegisterClass);
05160       return std::make_pair(0U, PPC::GPRCRegisterClass);
05161     case 'f':
05162       if (VT == MVT::f32)
05163         return std::make_pair(0U, PPC::F4RCRegisterClass);
05164       else if (VT == MVT::f64)
05165         return std::make_pair(0U, PPC::F8RCRegisterClass);
05166       break;
05167     case 'v':
05168       return std::make_pair(0U, PPC::VRRCRegisterClass);
05169     case 'y':   // crrc
05170       return std::make_pair(0U, PPC::CRRCRegisterClass);
05171     }
05172   }
05173 
05174   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
05175 }
05176 
05177 
05178 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
05179 /// vector.  If it is invalid, don't add anything to Ops. If hasMemory is true
05180 /// it means one of the asm constraint of the inline asm instruction being
05181 /// processed is 'm'.
05182 void PPCTargetLowering::LowerAsmOperandForConstraint(SDValue Op, char Letter,
05183                                                      bool hasMemory,
05184                                                      std::vector<SDValue>&Ops,
05185                                                      SelectionDAG &DAG) const {
05186   SDValue Result(0,0);
05187   switch (Letter) {
05188   default: break;
05189   case 'I':
05190   case 'J':
05191   case 'K':
05192   case 'L':
05193   case 'M':
05194   case 'N':
05195   case 'O':
05196   case 'P': {
05197     ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op);
05198     if (!CST) return; // Must be an immediate to match.
05199     unsigned Value = CST->getZExtValue();
05200     switch (Letter) {
05201     default: llvm_unreachable("Unknown constraint letter!");
05202     case 'I':  // "I" is a signed 16-bit constant.
05203       if ((short)Value == (int)Value)
05204         Result = DAG.getTargetConstant(Value, Op.getValueType());
05205       break;
05206     case 'J':  // "J" is a constant with only the high-order 16 bits nonzero.
05207     case 'L':  // "L" is a signed 16-bit constant shifted left 16 bits.
05208       if ((short)Value == 0)
05209         Result = DAG.getTargetConstant(Value, Op.getValueType());
05210       break;
05211     case 'K':  // "K" is a constant with only the low-order 16 bits nonzero.
05212       if ((Value >> 16) == 0)
05213         Result = DAG.getTargetConstant(Value, Op.getValueType());
05214       break;
05215     case 'M':  // "M" is a constant that is greater than 31.
05216       if (Value > 31)
05217         Result = DAG.getTargetConstant(Value, Op.getValueType());
05218       break;
05219     case 'N':  // "N" is a positive constant that is an exact power of two.
05220       if ((int)Value > 0 && isPowerOf2_32(Value))
05221         Result = DAG.getTargetConstant(Value, Op.getValueType());
05222       break;
05223     case 'O':  // "O" is the constant zero.
05224       if (Value == 0)
05225         Result = DAG.getTargetConstant(Value, Op.getValueType());
05226       break;
05227     case 'P':  // "P" is a constant whose negation is a signed 16-bit constant.
05228       if ((short)-Value == (int)-Value)
05229         Result = DAG.getTargetConstant(Value, Op.getValueType());
05230       break;
05231     }
05232     break;
05233   }
05234   }
05235 
05236   if (Result.getNode()) {
05237     Ops.push_back(Result);
05238     return;
05239   }
05240 
05241   // Handle standard constraint letters.
05242   TargetLowering::LowerAsmOperandForConstraint(Op, Letter, hasMemory, Ops, DAG);
05243 }
05244 
05245 // isLegalAddressingMode - Return true if the addressing mode represented
05246 // by AM is legal for this target, for a load/store of the specified type.
05247 bool PPCTargetLowering::isLegalAddressingMode(const AddrMode &AM,
05248                                               const Type *Ty) const {
05249   // FIXME: PPC does not allow r+i addressing modes for vectors!
05250 
05251   // PPC allows a sign-extended 16-bit immediate field.
05252   if (AM.BaseOffs <= -(1LL << 16) || AM.BaseOffs >= (1LL << 16)-1)
05253     return false;
05254 
05255   // No global is ever allowed as a base.
05256   if (AM.BaseGV)
05257     return false;
05258 
05259   // PPC only support r+r,
05260   switch (AM.Scale) {
05261   case 0:  // "r+i" or just "i", depending on HasBaseReg.
05262     break;
05263   case 1:
05264     if (AM.HasBaseReg && AM.BaseOffs)  // "r+r+i" is not allowed.
05265       return false;
05266     // Otherwise we have r+r or r+i.
05267     break;
05268   case 2:
05269     if (AM.HasBaseReg || AM.BaseOffs)  // 2*r+r  or  2*r+i is not allowed.
05270       return false;
05271     // Allow 2*r as r+r.
05272     break;
05273   default:
05274     // No other scales are supported.
05275     return false;
05276   }
05277 
05278   return true;
05279 }
05280 
05281 /// isLegalAddressImmediate - Return true if the integer value can be used
05282 /// as the offset of the target addressing mode for load / store of the
05283 /// given type.
05284 bool PPCTargetLowering::isLegalAddressImmediate(int64_t V,const Type *Ty) const{
05285   // PPC allows a sign-extended 16-bit immediate field.
05286   return (V > -(1 << 16) && V < (1 << 16)-1);
05287 }
05288 
05289 bool PPCTargetLowering::isLegalAddressImmediate(llvm::GlobalValue* GV) const {
05290   return false;
05291 }
05292 
05293 SDValue PPCTargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
05294   DebugLoc dl = Op.getDebugLoc();
05295   // Depths > 0 not supported yet!
05296   if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
05297     return SDValue();
05298 
05299   MachineFunction &MF = DAG.getMachineFunction();
05300   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
05301 
05302   // Just load the return address off the stack.
05303   SDValue RetAddrFI = getReturnAddrFrameIndex(DAG);
05304 
05305   // Make sure the function really does not optimize away the store of the RA
05306   // to the stack.
05307   FuncInfo->setLRStoreRequired();
05308   return DAG.getLoad(getPointerTy(), dl,
05309                      DAG.getEntryNode(), RetAddrFI, NULL, 0);
05310 }
05311 
05312 SDValue PPCTargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
05313   DebugLoc dl = Op.getDebugLoc();
05314   // Depths > 0 not supported yet!
05315   if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
05316     return SDValue();
05317 
05318   EVT PtrVT = DAG.getTargetLoweringInfo().getPointerTy();
05319   bool isPPC64 = PtrVT == MVT::i64;
05320 
05321   MachineFunction &MF = DAG.getMachineFunction();
05322   MachineFrameInfo *MFI = MF.getFrameInfo();
05323   bool is31 = (NoFramePointerElim || MFI->hasVarSizedObjects())
05324                   && MFI->getStackSize();
05325 
05326   if (isPPC64)
05327     return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::X31 : PPC::X1,
05328       MVT::i64);
05329   else
05330     return DAG.getCopyFromReg(DAG.getEntryNode(), dl, is31 ? PPC::R31 : PPC::R1,
05331       MVT::i32);
05332 }
05333 
05334 bool
05335 PPCTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
05336   // The PowerPC target isn't yet aware of offsets.
05337   return false;
05338 }
05339 
05340 EVT PPCTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
05341                                            bool isSrcConst, bool isSrcStr,
05342                                            SelectionDAG &DAG) const {
05343   if (this->PPCSubTarget.isPPC64()) {
05344     return MVT::i64;
05345   } else {
05346     return MVT::i32;
05347   }
05348 }