LLVM API Documentation

MipsISelLowering.cpp
Go to the documentation of this file.
00001 //===-- MipsISelLowering.cpp - Mips 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 defines the interfaces that Mips uses to lower LLVM code into a
00011 // selection DAG.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 #define DEBUG_TYPE "mips-lower"
00015 #include "MipsISelLowering.h"
00016 #include "InstPrinter/MipsInstPrinter.h"
00017 #include "MCTargetDesc/MipsBaseInfo.h"
00018 #include "MipsMachineFunction.h"
00019 #include "MipsSubtarget.h"
00020 #include "MipsTargetMachine.h"
00021 #include "MipsTargetObjectFile.h"
00022 #include "llvm/ADT/Statistic.h"
00023 #include "llvm/CodeGen/CallingConvLower.h"
00024 #include "llvm/CodeGen/MachineFrameInfo.h"
00025 #include "llvm/CodeGen/MachineFunction.h"
00026 #include "llvm/CodeGen/MachineInstrBuilder.h"
00027 #include "llvm/CodeGen/MachineRegisterInfo.h"
00028 #include "llvm/CodeGen/SelectionDAGISel.h"
00029 #include "llvm/CodeGen/ValueTypes.h"
00030 #include "llvm/IR/CallingConv.h"
00031 #include "llvm/IR/DerivedTypes.h"
00032 #include "llvm/IR/GlobalVariable.h"
00033 #include "llvm/Support/CommandLine.h"
00034 #include "llvm/Support/Debug.h"
00035 #include "llvm/Support/ErrorHandling.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 
00038 using namespace llvm;
00039 
00040 STATISTIC(NumTailCalls, "Number of tail calls");
00041 
00042 static cl::opt<bool>
00043 LargeGOT("mxgot", cl::Hidden,
00044          cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
00045 
00046 static cl::opt<bool>
00047 NoZeroDivCheck("mnocheck-zero-division", cl::Hidden,
00048                cl::desc("MIPS: Don't trap on integer division by zero."),
00049                cl::init(false));
00050 
00051 static const uint16_t O32IntRegs[4] = {
00052   Mips::A0, Mips::A1, Mips::A2, Mips::A3
00053 };
00054 
00055 static const uint16_t Mips64IntRegs[8] = {
00056   Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
00057   Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
00058 };
00059 
00060 static const uint16_t Mips64DPRegs[8] = {
00061   Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
00062   Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
00063 };
00064 
00065 // If I is a shifted mask, set the size (Size) and the first bit of the
00066 // mask (Pos), and return true.
00067 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
00068 static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
00069   if (!isShiftedMask_64(I))
00070      return false;
00071 
00072   Size = CountPopulation_64(I);
00073   Pos = CountTrailingZeros_64(I);
00074   return true;
00075 }
00076 
00077 SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
00078   MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
00079   return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
00080 }
00081 
00082 static SDValue getTargetNode(SDValue Op, SelectionDAG &DAG, unsigned Flag) {
00083   EVT Ty = Op.getValueType();
00084 
00085   if (GlobalAddressSDNode *N = dyn_cast<GlobalAddressSDNode>(Op))
00086     return DAG.getTargetGlobalAddress(N->getGlobal(), Op.getDebugLoc(), Ty, 0,
00087                                       Flag);
00088   if (ExternalSymbolSDNode *N = dyn_cast<ExternalSymbolSDNode>(Op))
00089     return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
00090   if (BlockAddressSDNode *N = dyn_cast<BlockAddressSDNode>(Op))
00091     return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
00092   if (JumpTableSDNode *N = dyn_cast<JumpTableSDNode>(Op))
00093     return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
00094   if (ConstantPoolSDNode *N = dyn_cast<ConstantPoolSDNode>(Op))
00095     return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
00096                                      N->getOffset(), Flag);
00097 
00098   llvm_unreachable("Unexpected node type.");
00099   return SDValue();
00100 }
00101 
00102 static SDValue getAddrNonPIC(SDValue Op, SelectionDAG &DAG) {
00103   DebugLoc DL = Op.getDebugLoc();
00104   EVT Ty = Op.getValueType();
00105   SDValue Hi = getTargetNode(Op, DAG, MipsII::MO_ABS_HI);
00106   SDValue Lo = getTargetNode(Op, DAG, MipsII::MO_ABS_LO);
00107   return DAG.getNode(ISD::ADD, DL, Ty,
00108                      DAG.getNode(MipsISD::Hi, DL, Ty, Hi),
00109                      DAG.getNode(MipsISD::Lo, DL, Ty, Lo));
00110 }
00111 
00112 SDValue MipsTargetLowering::getAddrLocal(SDValue Op, SelectionDAG &DAG,
00113                                          bool HasMips64) const {
00114   DebugLoc DL = Op.getDebugLoc();
00115   EVT Ty = Op.getValueType();
00116   unsigned GOTFlag = HasMips64 ? MipsII::MO_GOT_PAGE : MipsII::MO_GOT;
00117   SDValue GOT = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
00118                             getTargetNode(Op, DAG, GOTFlag));
00119   SDValue Load = DAG.getLoad(Ty, DL, DAG.getEntryNode(), GOT,
00120                              MachinePointerInfo::getGOT(), false, false, false,
00121                              0);
00122   unsigned LoFlag = HasMips64 ? MipsII::MO_GOT_OFST : MipsII::MO_ABS_LO;
00123   SDValue Lo = DAG.getNode(MipsISD::Lo, DL, Ty, getTargetNode(Op, DAG, LoFlag));
00124   return DAG.getNode(ISD::ADD, DL, Ty, Load, Lo);
00125 }
00126 
00127 SDValue MipsTargetLowering::getAddrGlobal(SDValue Op, SelectionDAG &DAG,
00128                                           unsigned Flag) const {
00129   DebugLoc DL = Op.getDebugLoc();
00130   EVT Ty = Op.getValueType();
00131   SDValue Tgt = DAG.getNode(MipsISD::Wrapper, DL, Ty, getGlobalReg(DAG, Ty),
00132                             getTargetNode(Op, DAG, Flag));
00133   return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Tgt,
00134                      MachinePointerInfo::getGOT(), false, false, false, 0);
00135 }
00136 
00137 SDValue MipsTargetLowering::getAddrGlobalLargeGOT(SDValue Op, SelectionDAG &DAG,
00138                                                   unsigned HiFlag,
00139                                                   unsigned LoFlag) const {
00140   DebugLoc DL = Op.getDebugLoc();
00141   EVT Ty = Op.getValueType();
00142   SDValue Hi = DAG.getNode(MipsISD::Hi, DL, Ty, getTargetNode(Op, DAG, HiFlag));
00143   Hi = DAG.getNode(ISD::ADD, DL, Ty, Hi, getGlobalReg(DAG, Ty));
00144   SDValue Wrapper = DAG.getNode(MipsISD::Wrapper, DL, Ty, Hi,
00145                                 getTargetNode(Op, DAG, LoFlag));
00146   return DAG.getLoad(Ty, DL, DAG.getEntryNode(), Wrapper,
00147                      MachinePointerInfo::getGOT(), false, false, false, 0);
00148 }
00149 
00150 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
00151   switch (Opcode) {
00152   case MipsISD::JmpLink:           return "MipsISD::JmpLink";
00153   case MipsISD::TailCall:          return "MipsISD::TailCall";
00154   case MipsISD::Hi:                return "MipsISD::Hi";
00155   case MipsISD::Lo:                return "MipsISD::Lo";
00156   case MipsISD::GPRel:             return "MipsISD::GPRel";
00157   case MipsISD::ThreadPointer:     return "MipsISD::ThreadPointer";
00158   case MipsISD::Ret:               return "MipsISD::Ret";
00159   case MipsISD::EH_RETURN:         return "MipsISD::EH_RETURN";
00160   case MipsISD::FPBrcond:          return "MipsISD::FPBrcond";
00161   case MipsISD::FPCmp:             return "MipsISD::FPCmp";
00162   case MipsISD::CMovFP_T:          return "MipsISD::CMovFP_T";
00163   case MipsISD::CMovFP_F:          return "MipsISD::CMovFP_F";
00164   case MipsISD::TruncIntFP:        return "MipsISD::TruncIntFP";
00165   case MipsISD::ExtractLOHI:       return "MipsISD::ExtractLOHI";
00166   case MipsISD::InsertLOHI:        return "MipsISD::InsertLOHI";
00167   case MipsISD::Mult:              return "MipsISD::Mult";
00168   case MipsISD::Multu:             return "MipsISD::Multu";
00169   case MipsISD::MAdd:              return "MipsISD::MAdd";
00170   case MipsISD::MAddu:             return "MipsISD::MAddu";
00171   case MipsISD::MSub:              return "MipsISD::MSub";
00172   case MipsISD::MSubu:             return "MipsISD::MSubu";
00173   case MipsISD::DivRem:            return "MipsISD::DivRem";
00174   case MipsISD::DivRemU:           return "MipsISD::DivRemU";
00175   case MipsISD::DivRem16:          return "MipsISD::DivRem16";
00176   case MipsISD::DivRemU16:         return "MipsISD::DivRemU16";
00177   case MipsISD::BuildPairF64:      return "MipsISD::BuildPairF64";
00178   case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
00179   case MipsISD::Wrapper:           return "MipsISD::Wrapper";
00180   case MipsISD::Sync:              return "MipsISD::Sync";
00181   case MipsISD::Ext:               return "MipsISD::Ext";
00182   case MipsISD::Ins:               return "MipsISD::Ins";
00183   case MipsISD::LWL:               return "MipsISD::LWL";
00184   case MipsISD::LWR:               return "MipsISD::LWR";
00185   case MipsISD::SWL:               return "MipsISD::SWL";
00186   case MipsISD::SWR:               return "MipsISD::SWR";
00187   case MipsISD::LDL:               return "MipsISD::LDL";
00188   case MipsISD::LDR:               return "MipsISD::LDR";
00189   case MipsISD::SDL:               return "MipsISD::SDL";
00190   case MipsISD::SDR:               return "MipsISD::SDR";
00191   case MipsISD::EXTP:              return "MipsISD::EXTP";
00192   case MipsISD::EXTPDP:            return "MipsISD::EXTPDP";
00193   case MipsISD::EXTR_S_H:          return "MipsISD::EXTR_S_H";
00194   case MipsISD::EXTR_W:            return "MipsISD::EXTR_W";
00195   case MipsISD::EXTR_R_W:          return "MipsISD::EXTR_R_W";
00196   case MipsISD::EXTR_RS_W:         return "MipsISD::EXTR_RS_W";
00197   case MipsISD::SHILO:             return "MipsISD::SHILO";
00198   case MipsISD::MTHLIP:            return "MipsISD::MTHLIP";
00199   case MipsISD::MULT:              return "MipsISD::MULT";
00200   case MipsISD::MULTU:             return "MipsISD::MULTU";
00201   case MipsISD::MADD_DSP:          return "MipsISD::MADD_DSP";
00202   case MipsISD::MADDU_DSP:         return "MipsISD::MADDU_DSP";
00203   case MipsISD::MSUB_DSP:          return "MipsISD::MSUB_DSP";
00204   case MipsISD::MSUBU_DSP:         return "MipsISD::MSUBU_DSP";
00205   case MipsISD::SHLL_DSP:          return "MipsISD::SHLL_DSP";
00206   case MipsISD::SHRA_DSP:          return "MipsISD::SHRA_DSP";
00207   case MipsISD::SHRL_DSP:          return "MipsISD::SHRL_DSP";
00208   case MipsISD::SETCC_DSP:         return "MipsISD::SETCC_DSP";
00209   case MipsISD::SELECT_CC_DSP:     return "MipsISD::SELECT_CC_DSP";
00210   default:                         return NULL;
00211   }
00212 }
00213 
00214 MipsTargetLowering::
00215 MipsTargetLowering(MipsTargetMachine &TM)
00216   : TargetLowering(TM, new MipsTargetObjectFile()),
00217     Subtarget(&TM.getSubtarget<MipsSubtarget>()),
00218     HasMips64(Subtarget->hasMips64()), IsN64(Subtarget->isABI_N64()),
00219     IsO32(Subtarget->isABI_O32()) {
00220   // Mips does not have i1 type, so use i32 for
00221   // setcc operations results (slt, sgt, ...).
00222   setBooleanContents(ZeroOrOneBooleanContent);
00223   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
00224 
00225   // Load extented operations for i1 types must be promoted
00226   setLoadExtAction(ISD::EXTLOAD,  MVT::i1,  Promote);
00227   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
00228   setLoadExtAction(ISD::SEXTLOAD, MVT::i1,  Promote);
00229 
00230   // MIPS doesn't have extending float->double load/store
00231   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
00232   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
00233 
00234   // Used by legalize types to correctly generate the setcc result.
00235   // Without this, every float setcc comes with a AND/OR with the result,
00236   // we don't want this, since the fpcmp result goes to a flag register,
00237   // which is used implicitly by brcond and select operations.
00238   AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
00239 
00240   // Mips Custom Operations
00241   setOperationAction(ISD::BR_JT,              MVT::Other, Custom);
00242   setOperationAction(ISD::GlobalAddress,      MVT::i32,   Custom);
00243   setOperationAction(ISD::BlockAddress,       MVT::i32,   Custom);
00244   setOperationAction(ISD::GlobalTLSAddress,   MVT::i32,   Custom);
00245   setOperationAction(ISD::JumpTable,          MVT::i32,   Custom);
00246   setOperationAction(ISD::ConstantPool,       MVT::i32,   Custom);
00247   setOperationAction(ISD::SELECT,             MVT::f32,   Custom);
00248   setOperationAction(ISD::SELECT,             MVT::f64,   Custom);
00249   setOperationAction(ISD::SELECT,             MVT::i32,   Custom);
00250   setOperationAction(ISD::SELECT_CC,          MVT::f32,   Custom);
00251   setOperationAction(ISD::SELECT_CC,          MVT::f64,   Custom);
00252   setOperationAction(ISD::SETCC,              MVT::f32,   Custom);
00253   setOperationAction(ISD::SETCC,              MVT::f64,   Custom);
00254   setOperationAction(ISD::BRCOND,             MVT::Other, Custom);
00255   setOperationAction(ISD::VASTART,            MVT::Other, Custom);
00256   setOperationAction(ISD::FCOPYSIGN,          MVT::f32,   Custom);
00257   setOperationAction(ISD::FCOPYSIGN,          MVT::f64,   Custom);
00258   setOperationAction(ISD::FP_TO_SINT,         MVT::i32,   Custom);
00259 
00260   if (!TM.Options.NoNaNsFPMath) {
00261     setOperationAction(ISD::FABS,             MVT::f32,   Custom);
00262     setOperationAction(ISD::FABS,             MVT::f64,   Custom);
00263   }
00264 
00265   if (HasMips64) {
00266     setOperationAction(ISD::GlobalAddress,      MVT::i64,   Custom);
00267     setOperationAction(ISD::BlockAddress,       MVT::i64,   Custom);
00268     setOperationAction(ISD::GlobalTLSAddress,   MVT::i64,   Custom);
00269     setOperationAction(ISD::JumpTable,          MVT::i64,   Custom);
00270     setOperationAction(ISD::ConstantPool,       MVT::i64,   Custom);
00271     setOperationAction(ISD::SELECT,             MVT::i64,   Custom);
00272     setOperationAction(ISD::LOAD,               MVT::i64,   Custom);
00273     setOperationAction(ISD::STORE,              MVT::i64,   Custom);
00274     setOperationAction(ISD::FP_TO_SINT,         MVT::i64,   Custom);
00275   }
00276 
00277   if (!HasMips64) {
00278     setOperationAction(ISD::SHL_PARTS,          MVT::i32,   Custom);
00279     setOperationAction(ISD::SRA_PARTS,          MVT::i32,   Custom);
00280     setOperationAction(ISD::SRL_PARTS,          MVT::i32,   Custom);
00281   }
00282 
00283   setOperationAction(ISD::ADD,                MVT::i32,   Custom);
00284   if (HasMips64)
00285     setOperationAction(ISD::ADD,                MVT::i64,   Custom);
00286 
00287   setOperationAction(ISD::SDIV, MVT::i32, Expand);
00288   setOperationAction(ISD::SREM, MVT::i32, Expand);
00289   setOperationAction(ISD::UDIV, MVT::i32, Expand);
00290   setOperationAction(ISD::UREM, MVT::i32, Expand);
00291   setOperationAction(ISD::SDIV, MVT::i64, Expand);
00292   setOperationAction(ISD::SREM, MVT::i64, Expand);
00293   setOperationAction(ISD::UDIV, MVT::i64, Expand);
00294   setOperationAction(ISD::UREM, MVT::i64, Expand);
00295 
00296   // Operations not directly supported by Mips.
00297   setOperationAction(ISD::BR_CC,             MVT::f32,   Expand);
00298   setOperationAction(ISD::BR_CC,             MVT::f64,   Expand);
00299   setOperationAction(ISD::BR_CC,             MVT::i32,   Expand);
00300   setOperationAction(ISD::BR_CC,             MVT::i64,   Expand);
00301   setOperationAction(ISD::SELECT_CC,         MVT::Other, Expand);
00302   setOperationAction(ISD::UINT_TO_FP,        MVT::i32,   Expand);
00303   setOperationAction(ISD::UINT_TO_FP,        MVT::i64,   Expand);
00304   setOperationAction(ISD::FP_TO_UINT,        MVT::i32,   Expand);
00305   setOperationAction(ISD::FP_TO_UINT,        MVT::i64,   Expand);
00306   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1,    Expand);
00307   setOperationAction(ISD::CTPOP,             MVT::i32,   Expand);
00308   setOperationAction(ISD::CTPOP,             MVT::i64,   Expand);
00309   setOperationAction(ISD::CTTZ,              MVT::i32,   Expand);
00310   setOperationAction(ISD::CTTZ,              MVT::i64,   Expand);
00311   setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::i32,   Expand);
00312   setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::i64,   Expand);
00313   setOperationAction(ISD::CTLZ_ZERO_UNDEF,   MVT::i32,   Expand);
00314   setOperationAction(ISD::CTLZ_ZERO_UNDEF,   MVT::i64,   Expand);
00315   setOperationAction(ISD::ROTL,              MVT::i32,   Expand);
00316   setOperationAction(ISD::ROTL,              MVT::i64,   Expand);
00317   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32,  Expand);
00318   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64,  Expand);
00319 
00320   if (!Subtarget->hasMips32r2())
00321     setOperationAction(ISD::ROTR, MVT::i32,   Expand);
00322 
00323   if (!Subtarget->hasMips64r2())
00324     setOperationAction(ISD::ROTR, MVT::i64,   Expand);
00325 
00326   setOperationAction(ISD::FSIN,              MVT::f32,   Expand);
00327   setOperationAction(ISD::FSIN,              MVT::f64,   Expand);
00328   setOperationAction(ISD::FCOS,              MVT::f32,   Expand);
00329   setOperationAction(ISD::FCOS,              MVT::f64,   Expand);
00330   setOperationAction(ISD::FSINCOS,           MVT::f32,   Expand);
00331   setOperationAction(ISD::FSINCOS,           MVT::f64,   Expand);
00332   setOperationAction(ISD::FPOWI,             MVT::f32,   Expand);
00333   setOperationAction(ISD::FPOW,              MVT::f32,   Expand);
00334   setOperationAction(ISD::FPOW,              MVT::f64,   Expand);
00335   setOperationAction(ISD::FLOG,              MVT::f32,   Expand);
00336   setOperationAction(ISD::FLOG2,             MVT::f32,   Expand);
00337   setOperationAction(ISD::FLOG10,            MVT::f32,   Expand);
00338   setOperationAction(ISD::FEXP,              MVT::f32,   Expand);
00339   setOperationAction(ISD::FMA,               MVT::f32,   Expand);
00340   setOperationAction(ISD::FMA,               MVT::f64,   Expand);
00341   setOperationAction(ISD::FREM,              MVT::f32,   Expand);
00342   setOperationAction(ISD::FREM,              MVT::f64,   Expand);
00343 
00344   if (!TM.Options.NoNaNsFPMath) {
00345     setOperationAction(ISD::FNEG,             MVT::f32,   Expand);
00346     setOperationAction(ISD::FNEG,             MVT::f64,   Expand);
00347   }
00348 
00349   setOperationAction(ISD::EXCEPTIONADDR,     MVT::i32, Expand);
00350   setOperationAction(ISD::EXCEPTIONADDR,     MVT::i64, Expand);
00351   setOperationAction(ISD::EHSELECTION,       MVT::i32, Expand);
00352   setOperationAction(ISD::EHSELECTION,       MVT::i64, Expand);
00353 
00354   setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
00355 
00356   setOperationAction(ISD::VAARG,             MVT::Other, Expand);
00357   setOperationAction(ISD::VACOPY,            MVT::Other, Expand);
00358   setOperationAction(ISD::VAEND,             MVT::Other, Expand);
00359 
00360   // Use the default for now
00361   setOperationAction(ISD::STACKSAVE,         MVT::Other, Expand);
00362   setOperationAction(ISD::STACKRESTORE,      MVT::Other, Expand);
00363 
00364   setOperationAction(ISD::ATOMIC_LOAD,       MVT::i32,    Expand);
00365   setOperationAction(ISD::ATOMIC_LOAD,       MVT::i64,    Expand);
00366   setOperationAction(ISD::ATOMIC_STORE,      MVT::i32,    Expand);
00367   setOperationAction(ISD::ATOMIC_STORE,      MVT::i64,    Expand);
00368 
00369   setInsertFencesForAtomic(true);
00370 
00371   if (!Subtarget->hasSEInReg()) {
00372     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
00373     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
00374   }
00375 
00376   if (!Subtarget->hasBitCount()) {
00377     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
00378     setOperationAction(ISD::CTLZ, MVT::i64, Expand);
00379   }
00380 
00381   if (!Subtarget->hasSwap()) {
00382     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
00383     setOperationAction(ISD::BSWAP, MVT::i64, Expand);
00384   }
00385 
00386   if (HasMips64) {
00387     setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
00388     setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
00389     setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
00390     setTruncStoreAction(MVT::i64, MVT::i32, Custom);
00391   }
00392 
00393   setTargetDAGCombine(ISD::SDIVREM);
00394   setTargetDAGCombine(ISD::UDIVREM);
00395   setTargetDAGCombine(ISD::SELECT);
00396   setTargetDAGCombine(ISD::AND);
00397   setTargetDAGCombine(ISD::OR);
00398   setTargetDAGCombine(ISD::ADD);
00399 
00400   setMinFunctionAlignment(HasMips64 ? 3 : 2);
00401 
00402   setStackPointerRegisterToSaveRestore(IsN64 ? Mips::SP_64 : Mips::SP);
00403 
00404   setExceptionPointerRegister(IsN64 ? Mips::A0_64 : Mips::A0);
00405   setExceptionSelectorRegister(IsN64 ? Mips::A1_64 : Mips::A1);
00406 
00407   MaxStoresPerMemcpy = 16;
00408 }
00409 
00410 const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
00411   if (TM.getSubtargetImpl()->inMips16Mode())
00412     return llvm::createMips16TargetLowering(TM);
00413 
00414   return llvm::createMipsSETargetLowering(TM);
00415 }
00416 
00417 EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
00418   if (!VT.isVector())
00419     return MVT::i32;
00420   return VT.changeVectorElementTypeToInteger();
00421 }
00422 
00423 static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
00424                                     TargetLowering::DAGCombinerInfo &DCI,
00425                                     const MipsSubtarget *Subtarget) {
00426   if (DCI.isBeforeLegalizeOps())
00427     return SDValue();
00428 
00429   EVT Ty = N->getValueType(0);
00430   unsigned LO = (Ty == MVT::i32) ? Mips::LO : Mips::LO64;
00431   unsigned HI = (Ty == MVT::i32) ? Mips::HI : Mips::HI64;
00432   unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
00433                                                   MipsISD::DivRemU16;
00434   DebugLoc DL = N->getDebugLoc();
00435 
00436   SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
00437                                N->getOperand(0), N->getOperand(1));
00438   SDValue InChain = DAG.getEntryNode();
00439   SDValue InGlue = DivRem;
00440 
00441   // insert MFLO
00442   if (N->hasAnyUseOfValue(0)) {
00443     SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
00444                                             InGlue);
00445     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
00446     InChain = CopyFromLo.getValue(1);
00447     InGlue = CopyFromLo.getValue(2);
00448   }
00449 
00450   // insert MFHI
00451   if (N->hasAnyUseOfValue(1)) {
00452     SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
00453                                             HI, Ty, InGlue);
00454     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
00455   }
00456 
00457   return SDValue();
00458 }
00459 
00460 static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
00461   switch (CC) {
00462   default: llvm_unreachable("Unknown fp condition code!");
00463   case ISD::SETEQ:
00464   case ISD::SETOEQ: return Mips::FCOND_OEQ;
00465   case ISD::SETUNE: return Mips::FCOND_UNE;
00466   case ISD::SETLT:
00467   case ISD::SETOLT: return Mips::FCOND_OLT;
00468   case ISD::SETGT:
00469   case ISD::SETOGT: return Mips::FCOND_OGT;
00470   case ISD::SETLE:
00471   case ISD::SETOLE: return Mips::FCOND_OLE;
00472   case ISD::SETGE:
00473   case ISD::SETOGE: return Mips::FCOND_OGE;
00474   case ISD::SETULT: return Mips::FCOND_ULT;
00475   case ISD::SETULE: return Mips::FCOND_ULE;
00476   case ISD::SETUGT: return Mips::FCOND_UGT;
00477   case ISD::SETUGE: return Mips::FCOND_UGE;
00478   case ISD::SETUO:  return Mips::FCOND_UN;
00479   case ISD::SETO:   return Mips::FCOND_OR;
00480   case ISD::SETNE:
00481   case ISD::SETONE: return Mips::FCOND_ONE;
00482   case ISD::SETUEQ: return Mips::FCOND_UEQ;
00483   }
00484 }
00485 
00486 
00487 /// This function returns true if the floating point conditional branches and
00488 /// conditional moves which use condition code CC should be inverted.
00489 static bool invertFPCondCodeUser(Mips::CondCode CC) {
00490   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
00491     return false;
00492 
00493   assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
00494          "Illegal Condition Code");
00495 
00496   return true;
00497 }
00498 
00499 // Creates and returns an FPCmp node from a setcc node.
00500 // Returns Op if setcc is not a floating point comparison.
00501 static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
00502   // must be a SETCC node
00503   if (Op.getOpcode() != ISD::SETCC)
00504     return Op;
00505 
00506   SDValue LHS = Op.getOperand(0);
00507 
00508   if (!LHS.getValueType().isFloatingPoint())
00509     return Op;
00510 
00511   SDValue RHS = Op.getOperand(1);
00512   DebugLoc DL = Op.getDebugLoc();
00513 
00514   // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
00515   // node if necessary.
00516   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
00517 
00518   return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
00519                      DAG.getConstant(condCodeToFCC(CC), MVT::i32));
00520 }
00521 
00522 // Creates and returns a CMovFPT/F node.
00523 static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
00524                             SDValue False, DebugLoc DL) {
00525   ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
00526   bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
00527 
00528   return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
00529                      True.getValueType(), True, False, Cond);
00530 }
00531 
00532 static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
00533                                     TargetLowering::DAGCombinerInfo &DCI,
00534                                     const MipsSubtarget *Subtarget) {
00535   if (DCI.isBeforeLegalizeOps())
00536     return SDValue();
00537 
00538   SDValue SetCC = N->getOperand(0);
00539 
00540   if ((SetCC.getOpcode() != ISD::SETCC) ||
00541       !SetCC.getOperand(0).getValueType().isInteger())
00542     return SDValue();
00543 
00544   SDValue False = N->getOperand(2);
00545   EVT FalseTy = False.getValueType();
00546 
00547   if (!FalseTy.isInteger())
00548     return SDValue();
00549 
00550   ConstantSDNode *CN = dyn_cast<ConstantSDNode>(False);
00551 
00552   if (!CN || CN->getZExtValue())
00553     return SDValue();
00554 
00555   const DebugLoc DL = N->getDebugLoc();
00556   ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
00557   SDValue True = N->getOperand(1);
00558 
00559   SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
00560                        SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
00561 
00562   return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
00563 }
00564 
00565 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
00566                                  TargetLowering::DAGCombinerInfo &DCI,
00567                                  const MipsSubtarget *Subtarget) {
00568   // Pattern match EXT.
00569   //  $dst = and ((sra or srl) $src , pos), (2**size - 1)
00570   //  => ext $dst, $src, size, pos
00571   if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
00572     return SDValue();
00573 
00574   SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
00575   unsigned ShiftRightOpc = ShiftRight.getOpcode();
00576 
00577   // Op's first operand must be a shift right.
00578   if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
00579     return SDValue();
00580 
00581   // The second operand of the shift must be an immediate.
00582   ConstantSDNode *CN;
00583   if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
00584     return SDValue();
00585 
00586   uint64_t Pos = CN->getZExtValue();
00587   uint64_t SMPos, SMSize;
00588 
00589   // Op's second operand must be a shifted mask.
00590   if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
00591       !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
00592     return SDValue();
00593 
00594   // Return if the shifted mask does not start at bit 0 or the sum of its size
00595   // and Pos exceeds the word's size.
00596   EVT ValTy = N->getValueType(0);
00597   if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
00598     return SDValue();
00599 
00600   return DAG.getNode(MipsISD::Ext, N->getDebugLoc(), ValTy,
00601                      ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
00602                      DAG.getConstant(SMSize, MVT::i32));
00603 }
00604 
00605 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
00606                                 TargetLowering::DAGCombinerInfo &DCI,
00607                                 const MipsSubtarget *Subtarget) {
00608   // Pattern match INS.
00609   //  $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
00610   //  where mask1 = (2**size - 1) << pos, mask0 = ~mask1
00611   //  => ins $dst, $src, size, pos, $src1
00612   if (DCI.isBeforeLegalizeOps() || !Subtarget->hasMips32r2())
00613     return SDValue();
00614 
00615   SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
00616   uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
00617   ConstantSDNode *CN;
00618 
00619   // See if Op's first operand matches (and $src1 , mask0).
00620   if (And0.getOpcode() != ISD::AND)
00621     return SDValue();
00622 
00623   if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
00624       !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
00625     return SDValue();
00626 
00627   // See if Op's second operand matches (and (shl $src, pos), mask1).
00628   if (And1.getOpcode() != ISD::AND)
00629     return SDValue();
00630 
00631   if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
00632       !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
00633     return SDValue();
00634 
00635   // The shift masks must have the same position and size.
00636   if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
00637     return SDValue();
00638 
00639   SDValue Shl = And1.getOperand(0);
00640   if (Shl.getOpcode() != ISD::SHL)
00641     return SDValue();
00642 
00643   if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
00644     return SDValue();
00645 
00646   unsigned Shamt = CN->getZExtValue();
00647 
00648   // Return if the shift amount and the first bit position of mask are not the
00649   // same.
00650   EVT ValTy = N->getValueType(0);
00651   if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
00652     return SDValue();
00653 
00654   return DAG.getNode(MipsISD::Ins, N->getDebugLoc(), ValTy, Shl.getOperand(0),
00655                      DAG.getConstant(SMPos0, MVT::i32),
00656                      DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
00657 }
00658 
00659 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
00660                                  TargetLowering::DAGCombinerInfo &DCI,
00661                                  const MipsSubtarget *Subtarget) {
00662   // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
00663 
00664   if (DCI.isBeforeLegalizeOps())
00665     return SDValue();
00666 
00667   SDValue Add = N->getOperand(1);
00668 
00669   if (Add.getOpcode() != ISD::ADD)
00670     return SDValue();
00671 
00672   SDValue Lo = Add.getOperand(1);
00673 
00674   if ((Lo.getOpcode() != MipsISD::Lo) ||
00675       (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
00676     return SDValue();
00677 
00678   EVT ValTy = N->getValueType(0);
00679   DebugLoc DL = N->getDebugLoc();
00680 
00681   SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
00682                              Add.getOperand(0));
00683   return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
00684 }
00685 
00686 SDValue  MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
00687   const {
00688   SelectionDAG &DAG = DCI.DAG;
00689   unsigned Opc = N->getOpcode();
00690 
00691   switch (Opc) {
00692   default: break;
00693   case ISD::SDIVREM:
00694   case ISD::UDIVREM:
00695     return performDivRemCombine(N, DAG, DCI, Subtarget);
00696   case ISD::SELECT:
00697     return performSELECTCombine(N, DAG, DCI, Subtarget);
00698   case ISD::AND:
00699     return performANDCombine(N, DAG, DCI, Subtarget);
00700   case ISD::OR:
00701     return performORCombine(N, DAG, DCI, Subtarget);
00702   case ISD::ADD:
00703     return performADDCombine(N, DAG, DCI, Subtarget);
00704   }
00705 
00706   return SDValue();
00707 }
00708 
00709 void
00710 MipsTargetLowering::LowerOperationWrapper(SDNode *N,
00711                                           SmallVectorImpl<SDValue> &Results,
00712                                           SelectionDAG &DAG) const {
00713   SDValue Res = LowerOperation(SDValue(N, 0), DAG);
00714 
00715   for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
00716     Results.push_back(Res.getValue(I));
00717 }
00718 
00719 void
00720 MipsTargetLowering::ReplaceNodeResults(SDNode *N,
00721                                        SmallVectorImpl<SDValue> &Results,
00722                                        SelectionDAG &DAG) const {
00723   return LowerOperationWrapper(N, Results, DAG);
00724 }
00725 
00726 SDValue MipsTargetLowering::
00727 LowerOperation(SDValue Op, SelectionDAG &DAG) const
00728 {
00729   switch (Op.getOpcode())
00730   {
00731   case ISD::BR_JT:              return lowerBR_JT(Op, DAG);
00732   case ISD::BRCOND:             return lowerBRCOND(Op, DAG);
00733   case ISD::ConstantPool:       return lowerConstantPool(Op, DAG);
00734   case ISD::GlobalAddress:      return lowerGlobalAddress(Op, DAG);
00735   case ISD::BlockAddress:       return lowerBlockAddress(Op, DAG);
00736   case ISD::GlobalTLSAddress:   return lowerGlobalTLSAddress(Op, DAG);
00737   case ISD::JumpTable:          return lowerJumpTable(Op, DAG);
00738   case ISD::SELECT:             return lowerSELECT(Op, DAG);
00739   case ISD::SELECT_CC:          return lowerSELECT_CC(Op, DAG);
00740   case ISD::SETCC:              return lowerSETCC(Op, DAG);
00741   case ISD::VASTART:            return lowerVASTART(Op, DAG);
00742   case ISD::FCOPYSIGN:          return lowerFCOPYSIGN(Op, DAG);
00743   case ISD::FABS:               return lowerFABS(Op, DAG);
00744   case ISD::FRAMEADDR:          return lowerFRAMEADDR(Op, DAG);
00745   case ISD::RETURNADDR:         return lowerRETURNADDR(Op, DAG);
00746   case ISD::EH_RETURN:          return lowerEH_RETURN(Op, DAG);
00747   case ISD::ATOMIC_FENCE:       return lowerATOMIC_FENCE(Op, DAG);
00748   case ISD::SHL_PARTS:          return lowerShiftLeftParts(Op, DAG);
00749   case ISD::SRA_PARTS:          return lowerShiftRightParts(Op, DAG, true);
00750   case ISD::SRL_PARTS:          return lowerShiftRightParts(Op, DAG, false);
00751   case ISD::LOAD:               return lowerLOAD(Op, DAG);
00752   case ISD::STORE:              return lowerSTORE(Op, DAG);
00753   case ISD::ADD:                return lowerADD(Op, DAG);
00754   case ISD::FP_TO_SINT:         return lowerFP_TO_SINT(Op, DAG);
00755   }
00756   return SDValue();
00757 }
00758 
00759 //===----------------------------------------------------------------------===//
00760 //  Lower helper functions
00761 //===----------------------------------------------------------------------===//
00762 
00763 // addLiveIn - This helper function adds the specified physical register to the
00764 // MachineFunction as a live in value.  It also creates a corresponding
00765 // virtual register for it.
00766 static unsigned
00767 addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
00768 {
00769   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
00770   MF.getRegInfo().addLiveIn(PReg, VReg);
00771   return VReg;
00772 }
00773 
00774 static MachineBasicBlock *expandPseudoDIV(MachineInstr *MI,
00775                                           MachineBasicBlock &MBB,
00776                                           const TargetInstrInfo &TII,
00777                                           bool Is64Bit) {
00778   if (NoZeroDivCheck)
00779     return &MBB;
00780 
00781   // Insert instruction "teq $divisor_reg, $zero, 7".
00782   MachineBasicBlock::iterator I(MI);
00783   MachineInstrBuilder MIB;
00784   MIB = BuildMI(MBB, llvm::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
00785     .addOperand(MI->getOperand(2)).addReg(Mips::ZERO).addImm(7);
00786 
00787   // Use the 32-bit sub-register if this is a 64-bit division.
00788   if (Is64Bit)
00789     MIB->getOperand(0).setSubReg(Mips::sub_32);
00790 
00791   return &MBB;
00792 }
00793 
00794 MachineBasicBlock *
00795 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
00796                                                 MachineBasicBlock *BB) const {
00797   switch (MI->getOpcode()) {
00798   default:
00799     llvm_unreachable("Unexpected instr type to insert");
00800   case Mips::ATOMIC_LOAD_ADD_I8:
00801   case Mips::ATOMIC_LOAD_ADD_I8_P8:
00802     return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
00803   case Mips::ATOMIC_LOAD_ADD_I16:
00804   case Mips::ATOMIC_LOAD_ADD_I16_P8:
00805     return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
00806   case Mips::ATOMIC_LOAD_ADD_I32:
00807   case Mips::ATOMIC_LOAD_ADD_I32_P8:
00808     return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
00809   case Mips::ATOMIC_LOAD_ADD_I64:
00810   case Mips::ATOMIC_LOAD_ADD_I64_P8:
00811     return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
00812 
00813   case Mips::ATOMIC_LOAD_AND_I8:
00814   case Mips::ATOMIC_LOAD_AND_I8_P8:
00815     return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
00816   case Mips::ATOMIC_LOAD_AND_I16:
00817   case Mips::ATOMIC_LOAD_AND_I16_P8:
00818     return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
00819   case Mips::ATOMIC_LOAD_AND_I32:
00820   case Mips::ATOMIC_LOAD_AND_I32_P8:
00821     return emitAtomicBinary(MI, BB, 4, Mips::AND);
00822   case Mips::ATOMIC_LOAD_AND_I64:
00823   case Mips::ATOMIC_LOAD_AND_I64_P8:
00824     return emitAtomicBinary(MI, BB, 8, Mips::AND64);
00825 
00826   case Mips::ATOMIC_LOAD_OR_I8:
00827   case Mips::ATOMIC_LOAD_OR_I8_P8:
00828     return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
00829   case Mips::ATOMIC_LOAD_OR_I16:
00830   case Mips::ATOMIC_LOAD_OR_I16_P8:
00831     return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
00832   case Mips::ATOMIC_LOAD_OR_I32:
00833   case Mips::ATOMIC_LOAD_OR_I32_P8:
00834     return emitAtomicBinary(MI, BB, 4, Mips::OR);
00835   case Mips::ATOMIC_LOAD_OR_I64:
00836   case Mips::ATOMIC_LOAD_OR_I64_P8:
00837     return emitAtomicBinary(MI, BB, 8, Mips::OR64);
00838 
00839   case Mips::ATOMIC_LOAD_XOR_I8:
00840   case Mips::ATOMIC_LOAD_XOR_I8_P8:
00841     return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
00842   case Mips::ATOMIC_LOAD_XOR_I16:
00843   case Mips::ATOMIC_LOAD_XOR_I16_P8:
00844     return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
00845   case Mips::ATOMIC_LOAD_XOR_I32:
00846   case Mips::ATOMIC_LOAD_XOR_I32_P8:
00847     return emitAtomicBinary(MI, BB, 4, Mips::XOR);
00848   case Mips::ATOMIC_LOAD_XOR_I64:
00849   case Mips::ATOMIC_LOAD_XOR_I64_P8:
00850     return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
00851 
00852   case Mips::ATOMIC_LOAD_NAND_I8:
00853   case Mips::ATOMIC_LOAD_NAND_I8_P8:
00854     return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
00855   case Mips::ATOMIC_LOAD_NAND_I16:
00856   case Mips::ATOMIC_LOAD_NAND_I16_P8:
00857     return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
00858   case Mips::ATOMIC_LOAD_NAND_I32:
00859   case Mips::ATOMIC_LOAD_NAND_I32_P8:
00860     return emitAtomicBinary(MI, BB, 4, 0, true);
00861   case Mips::ATOMIC_LOAD_NAND_I64:
00862   case Mips::ATOMIC_LOAD_NAND_I64_P8:
00863     return emitAtomicBinary(MI, BB, 8, 0, true);
00864 
00865   case Mips::ATOMIC_LOAD_SUB_I8:
00866   case Mips::ATOMIC_LOAD_SUB_I8_P8:
00867     return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
00868   case Mips::ATOMIC_LOAD_SUB_I16:
00869   case Mips::ATOMIC_LOAD_SUB_I16_P8:
00870     return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
00871   case Mips::ATOMIC_LOAD_SUB_I32:
00872   case Mips::ATOMIC_LOAD_SUB_I32_P8:
00873     return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
00874   case Mips::ATOMIC_LOAD_SUB_I64:
00875   case Mips::ATOMIC_LOAD_SUB_I64_P8:
00876     return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
00877 
00878   case Mips::ATOMIC_SWAP_I8:
00879   case Mips::ATOMIC_SWAP_I8_P8:
00880     return emitAtomicBinaryPartword(MI, BB, 1, 0);
00881   case Mips::ATOMIC_SWAP_I16:
00882   case Mips::ATOMIC_SWAP_I16_P8:
00883     return emitAtomicBinaryPartword(MI, BB, 2, 0);
00884   case Mips::ATOMIC_SWAP_I32:
00885   case Mips::ATOMIC_SWAP_I32_P8:
00886     return emitAtomicBinary(MI, BB, 4, 0);
00887   case Mips::ATOMIC_SWAP_I64:
00888   case Mips::ATOMIC_SWAP_I64_P8:
00889     return emitAtomicBinary(MI, BB, 8, 0);
00890 
00891   case Mips::ATOMIC_CMP_SWAP_I8:
00892   case Mips::ATOMIC_CMP_SWAP_I8_P8:
00893     return emitAtomicCmpSwapPartword(MI, BB, 1);
00894   case Mips::ATOMIC_CMP_SWAP_I16:
00895   case Mips::ATOMIC_CMP_SWAP_I16_P8:
00896     return emitAtomicCmpSwapPartword(MI, BB, 2);
00897   case Mips::ATOMIC_CMP_SWAP_I32:
00898   case Mips::ATOMIC_CMP_SWAP_I32_P8:
00899     return emitAtomicCmpSwap(MI, BB, 4);
00900   case Mips::ATOMIC_CMP_SWAP_I64:
00901   case Mips::ATOMIC_CMP_SWAP_I64_P8:
00902     return emitAtomicCmpSwap(MI, BB, 8);
00903   case Mips::PseudoSDIV:
00904   case Mips::PseudoUDIV:
00905     return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), false);
00906   case Mips::PseudoDSDIV:
00907   case Mips::PseudoDUDIV:
00908     return expandPseudoDIV(MI, *BB, *getTargetMachine().getInstrInfo(), true);
00909   }
00910 }
00911 
00912 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
00913 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
00914 MachineBasicBlock *
00915 MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
00916                                      unsigned Size, unsigned BinOpcode,
00917                                      bool Nand) const {
00918   assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
00919 
00920   MachineFunction *MF = BB->getParent();
00921   MachineRegisterInfo &RegInfo = MF->getRegInfo();
00922   const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
00923   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
00924   DebugLoc DL = MI->getDebugLoc();
00925   unsigned LL, SC, AND, NOR, ZERO, BEQ;
00926 
00927   if (Size == 4) {
00928     LL = IsN64 ? Mips::LL_P8 : Mips::LL;
00929     SC = IsN64 ? Mips::SC_P8 : Mips::SC;
00930     AND = Mips::AND;
00931     NOR = Mips::NOR;
00932     ZERO = Mips::ZERO;
00933     BEQ = Mips::BEQ;
00934   }
00935   else {
00936     LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
00937     SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
00938     AND = Mips::AND64;
00939     NOR = Mips::NOR64;
00940     ZERO = Mips::ZERO_64;
00941     BEQ = Mips::BEQ64;
00942   }
00943 
00944   unsigned OldVal = MI->getOperand(0).getReg();
00945   unsigned Ptr = MI->getOperand(1).getReg();
00946   unsigned Incr = MI->getOperand(2).getReg();
00947 
00948   unsigned StoreVal = RegInfo.createVirtualRegister(RC);
00949   unsigned AndRes = RegInfo.createVirtualRegister(RC);
00950   unsigned Success = RegInfo.createVirtualRegister(RC);
00951 
00952   // insert new blocks after the current block
00953   const BasicBlock *LLVM_BB = BB->getBasicBlock();
00954   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
00955   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
00956   MachineFunction::iterator It = BB;
00957   ++It;
00958   MF->insert(It, loopMBB);
00959   MF->insert(It, exitMBB);
00960 
00961   // Transfer the remainder of BB and its successor edges to exitMBB.
00962   exitMBB->splice(exitMBB->begin(), BB,
00963                   llvm::next(MachineBasicBlock::iterator(MI)),
00964                   BB->end());
00965   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
00966 
00967   //  thisMBB:
00968   //    ...
00969   //    fallthrough --> loopMBB
00970   BB->addSuccessor(loopMBB);
00971   loopMBB->addSuccessor(loopMBB);
00972   loopMBB->addSuccessor(exitMBB);
00973 
00974   //  loopMBB:
00975   //    ll oldval, 0(ptr)
00976   //    <binop> storeval, oldval, incr
00977   //    sc success, storeval, 0(ptr)
00978   //    beq success, $0, loopMBB
00979   BB = loopMBB;
00980   BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
00981   if (Nand) {
00982     //  and andres, oldval, incr
00983     //  nor storeval, $0, andres
00984     BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
00985     BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
00986   } else if (BinOpcode) {
00987     //  <binop> storeval, oldval, incr
00988     BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
00989   } else {
00990     StoreVal = Incr;
00991   }
00992   BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
00993   BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
00994 
00995   MI->eraseFromParent();   // The instruction is gone now.
00996 
00997   return exitMBB;
00998 }
00999 
01000 MachineBasicBlock *
01001 MipsTargetLowering::emitAtomicBinaryPartword(MachineInstr *MI,
01002                                              MachineBasicBlock *BB,
01003                                              unsigned Size, unsigned BinOpcode,
01004                                              bool Nand) const {
01005   assert((Size == 1 || Size == 2) &&
01006       "Unsupported size for EmitAtomicBinaryPartial.");
01007 
01008   MachineFunction *MF = BB->getParent();
01009   MachineRegisterInfo &RegInfo = MF->getRegInfo();
01010   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
01011   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
01012   DebugLoc DL = MI->getDebugLoc();
01013   unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
01014   unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
01015 
01016   unsigned Dest = MI->getOperand(0).getReg();
01017   unsigned Ptr = MI->getOperand(1).getReg();
01018   unsigned Incr = MI->getOperand(2).getReg();
01019 
01020   unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
01021   unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
01022   unsigned Mask = RegInfo.createVirtualRegister(RC);
01023   unsigned Mask2 = RegInfo.createVirtualRegister(RC);
01024   unsigned NewVal = RegInfo.createVirtualRegister(RC);
01025   unsigned OldVal = RegInfo.createVirtualRegister(RC);
01026   unsigned Incr2 = RegInfo.createVirtualRegister(RC);
01027   unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
01028   unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
01029   unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
01030   unsigned AndRes = RegInfo.createVirtualRegister(RC);
01031   unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
01032   unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
01033   unsigned StoreVal = RegInfo.createVirtualRegister(RC);
01034   unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
01035   unsigned SrlRes = RegInfo.createVirtualRegister(RC);
01036   unsigned SllRes = RegInfo.createVirtualRegister(RC);
01037   unsigned Success = RegInfo.createVirtualRegister(RC);
01038 
01039   // insert new blocks after the current block
01040   const BasicBlock *LLVM_BB = BB->getBasicBlock();
01041   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
01042   MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
01043   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
01044   MachineFunction::iterator It = BB;
01045   ++It;
01046   MF->insert(It, loopMBB);
01047   MF->insert(It, sinkMBB);
01048   MF->insert(It, exitMBB);
01049 
01050   // Transfer the remainder of BB and its successor edges to exitMBB.
01051   exitMBB->splice(exitMBB->begin(), BB,
01052                   llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
01053   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
01054 
01055   BB->addSuccessor(loopMBB);
01056   loopMBB->addSuccessor(loopMBB);
01057   loopMBB->addSuccessor(sinkMBB);
01058   sinkMBB->addSuccessor(exitMBB);
01059 
01060   //  thisMBB:
01061   //    addiu   masklsb2,$0,-4                # 0xfffffffc
01062   //    and     alignedaddr,ptr,masklsb2
01063   //    andi    ptrlsb2,ptr,3
01064   //    sll     shiftamt,ptrlsb2,3
01065   //    ori     maskupper,$0,255               # 0xff
01066   //    sll     mask,maskupper,shiftamt
01067   //    nor     mask2,$0,mask
01068   //    sll     incr2,incr,shiftamt
01069 
01070   int64_t MaskImm = (Size == 1) ? 255 : 65535;
01071   BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
01072     .addReg(Mips::ZERO).addImm(-4);
01073   BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
01074     .addReg(Ptr).addReg(MaskLSB2);
01075   BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
01076   BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
01077   BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
01078     .addReg(Mips::ZERO).addImm(MaskImm);
01079   BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
01080     .addReg(ShiftAmt).addReg(MaskUpper);
01081   BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
01082   BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(ShiftAmt).addReg(Incr);
01083 
01084   // atomic.load.binop
01085   // loopMBB:
01086   //   ll      oldval,0(alignedaddr)
01087   //   binop   binopres,oldval,incr2
01088   //   and     newval,binopres,mask
01089   //   and     maskedoldval0,oldval,mask2
01090   //   or      storeval,maskedoldval0,newval
01091   //   sc      success,storeval,0(alignedaddr)
01092   //   beq     success,$0,loopMBB
01093 
01094   // atomic.swap
01095   // loopMBB:
01096   //   ll      oldval,0(alignedaddr)
01097   //   and     newval,incr2,mask
01098   //   and     maskedoldval0,oldval,mask2
01099   //   or      storeval,maskedoldval0,newval
01100   //   sc      success,storeval,0(alignedaddr)
01101   //   beq     success,$0,loopMBB
01102 
01103   BB = loopMBB;
01104   BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
01105   if (Nand) {
01106     //  and andres, oldval, incr2
01107     //  nor binopres, $0, andres
01108     //  and newval, binopres, mask
01109     BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
01110     BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
01111       .addReg(Mips::ZERO).addReg(AndRes);
01112     BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
01113   } else if (BinOpcode) {
01114     //  <binop> binopres, oldval, incr2
01115     //  and newval, binopres, mask
01116     BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
01117     BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
01118   } else {// atomic.swap
01119     //  and newval, incr2, mask
01120     BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
01121   }
01122 
01123   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
01124     .addReg(OldVal).addReg(Mask2);
01125   BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
01126     .addReg(MaskedOldVal0).addReg(NewVal);
01127   BuildMI(BB, DL, TII->get(SC), Success)
01128     .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
01129   BuildMI(BB, DL, TII->get(Mips::BEQ))
01130     .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
01131 
01132   //  sinkMBB:
01133   //    and     maskedoldval1,oldval,mask
01134   //    srl     srlres,maskedoldval1,shiftamt
01135   //    sll     sllres,srlres,24
01136   //    sra     dest,sllres,24
01137   BB = sinkMBB;
01138   int64_t ShiftImm = (Size == 1) ? 24 : 16;
01139 
01140   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
01141     .addReg(OldVal).addReg(Mask);
01142   BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
01143       .addReg(ShiftAmt).addReg(MaskedOldVal1);
01144   BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
01145       .addReg(SrlRes).addImm(ShiftImm);
01146   BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
01147       .addReg(SllRes).addImm(ShiftImm);
01148 
01149   MI->eraseFromParent();   // The instruction is gone now.
01150 
01151   return exitMBB;
01152 }
01153 
01154 MachineBasicBlock *
01155 MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
01156                                       MachineBasicBlock *BB,
01157                                       unsigned Size) const {
01158   assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
01159 
01160   MachineFunction *MF = BB->getParent();
01161   MachineRegisterInfo &RegInfo = MF->getRegInfo();
01162   const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
01163   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
01164   DebugLoc DL = MI->getDebugLoc();
01165   unsigned LL, SC, ZERO, BNE, BEQ;
01166 
01167   if (Size == 4) {
01168     LL = IsN64 ? Mips::LL_P8 : Mips::LL;
01169     SC = IsN64 ? Mips::SC_P8 : Mips::SC;
01170     ZERO = Mips::ZERO;
01171     BNE = Mips::BNE;
01172     BEQ = Mips::BEQ;
01173   }
01174   else {
01175     LL = IsN64 ? Mips::LLD_P8 : Mips::LLD;
01176     SC = IsN64 ? Mips::SCD_P8 : Mips::SCD;
01177     ZERO = Mips::ZERO_64;
01178     BNE = Mips::BNE64;
01179     BEQ = Mips::BEQ64;
01180   }
01181 
01182   unsigned Dest    = MI->getOperand(0).getReg();
01183   unsigned Ptr     = MI->getOperand(1).getReg();
01184   unsigned OldVal  = MI->getOperand(2).getReg();
01185   unsigned NewVal  = MI->getOperand(3).getReg();
01186 
01187   unsigned Success = RegInfo.createVirtualRegister(RC);
01188 
01189   // insert new blocks after the current block
01190   const BasicBlock *LLVM_BB = BB->getBasicBlock();
01191   MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
01192   MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
01193   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
01194   MachineFunction::iterator It = BB;
01195   ++It;
01196   MF->insert(It, loop1MBB);
01197   MF->insert(It, loop2MBB);
01198   MF->insert(It, exitMBB);
01199 
01200   // Transfer the remainder of BB and its successor edges to exitMBB.
01201   exitMBB->splice(exitMBB->begin(), BB,
01202                   llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
01203   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
01204 
01205   //  thisMBB:
01206   //    ...
01207   //    fallthrough --> loop1MBB
01208   BB->addSuccessor(loop1MBB);
01209   loop1MBB->addSuccessor(exitMBB);
01210   loop1MBB->addSuccessor(loop2MBB);
01211   loop2MBB->addSuccessor(loop1MBB);
01212   loop2MBB->addSuccessor(exitMBB);
01213 
01214   // loop1MBB:
01215   //   ll dest, 0(ptr)
01216   //   bne dest, oldval, exitMBB
01217   BB = loop1MBB;
01218   BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
01219   BuildMI(BB, DL, TII->get(BNE))
01220     .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
01221 
01222   // loop2MBB:
01223   //   sc success, newval, 0(ptr)
01224   //   beq success, $0, loop1MBB
01225   BB = loop2MBB;
01226   BuildMI(BB, DL, TII->get(SC), Success)
01227     .addReg(NewVal).addReg(Ptr).addImm(0);
01228   BuildMI(BB, DL, TII->get(BEQ))
01229     .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
01230 
01231   MI->eraseFromParent();   // The instruction is gone now.
01232 
01233   return exitMBB;
01234 }
01235 
01236 MachineBasicBlock *
01237 MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
01238                                               MachineBasicBlock *BB,
01239                                               unsigned Size) const {
01240   assert((Size == 1 || Size == 2) &&
01241       "Unsupported size for EmitAtomicCmpSwapPartial.");
01242 
01243   MachineFunction *MF = BB->getParent();
01244   MachineRegisterInfo &RegInfo = MF->getRegInfo();
01245   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
01246   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
01247   DebugLoc DL = MI->getDebugLoc();
01248   unsigned LL = IsN64 ? Mips::LL_P8 : Mips::LL;
01249   unsigned SC = IsN64 ? Mips::SC_P8 : Mips::SC;
01250 
01251   unsigned Dest    = MI->getOperand(0).getReg();
01252   unsigned Ptr     = MI->getOperand(1).getReg();
01253   unsigned CmpVal  = MI->getOperand(2).getReg();
01254   unsigned NewVal  = MI->getOperand(3).getReg();
01255 
01256   unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
01257   unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
01258   unsigned Mask = RegInfo.createVirtualRegister(RC);
01259   unsigned Mask2 = RegInfo.createVirtualRegister(RC);
01260   unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
01261   unsigned OldVal = RegInfo.createVirtualRegister(RC);
01262   unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
01263   unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
01264   unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
01265   unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
01266   unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
01267   unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
01268   unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
01269   unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
01270   unsigned StoreVal = RegInfo.createVirtualRegister(RC);
01271   unsigned SrlRes = RegInfo.createVirtualRegister(RC);
01272   unsigned SllRes = RegInfo.createVirtualRegister(RC);
01273   unsigned Success = RegInfo.createVirtualRegister(RC);
01274 
01275   // insert new blocks after the current block
01276   const BasicBlock *LLVM_BB = BB->getBasicBlock();
01277   MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
01278   MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
01279   MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
01280   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
01281   MachineFunction::iterator It = BB;
01282   ++It;
01283   MF->insert(It, loop1MBB);
01284   MF->insert(It, loop2MBB);
01285   MF->insert(It, sinkMBB);
01286   MF->insert(It, exitMBB);
01287 
01288   // Transfer the remainder of BB and its successor edges to exitMBB.
01289   exitMBB->splice(exitMBB->begin(), BB,
01290                   llvm::next(MachineBasicBlock::iterator(MI)), BB->end());
01291   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
01292 
01293   BB->addSuccessor(loop1MBB);
01294   loop1MBB->addSuccessor(sinkMBB);
01295   loop1MBB->addSuccessor(loop2MBB);
01296   loop2MBB->addSuccessor(loop1MBB);
01297   loop2MBB->addSuccessor(sinkMBB);
01298   sinkMBB->addSuccessor(exitMBB);
01299 
01300   // FIXME: computation of newval2 can be moved to loop2MBB.
01301   //  thisMBB:
01302   //    addiu   masklsb2,$0,-4                # 0xfffffffc
01303   //    and     alignedaddr,ptr,masklsb2
01304   //    andi    ptrlsb2,ptr,3
01305   //    sll     shiftamt,ptrlsb2,3
01306   //    ori     maskupper,$0,255               # 0xff
01307   //    sll     mask,maskupper,shiftamt
01308   //    nor     mask2,$0,mask
01309   //    andi    maskedcmpval,cmpval,255
01310   //    sll     shiftedcmpval,maskedcmpval,shiftamt
01311   //    andi    maskednewval,newval,255
01312   //    sll     shiftednewval,maskednewval,shiftamt
01313   int64_t MaskImm = (Size == 1) ? 255 : 65535;
01314   BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
01315     .addReg(Mips::ZERO).addImm(-4);
01316   BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
01317     .addReg(Ptr).addReg(MaskLSB2);
01318   BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
01319   BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
01320   BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
01321     .addReg(Mips::ZERO).addImm(MaskImm);
01322   BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
01323     .addReg(ShiftAmt).addReg(MaskUpper);
01324   BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
01325   BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
01326     .addReg(CmpVal).addImm(MaskImm);
01327   BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
01328     .addReg(ShiftAmt).addReg(MaskedCmpVal);
01329   BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
01330     .addReg(NewVal).addImm(MaskImm);
01331   BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
01332     .addReg(ShiftAmt).addReg(MaskedNewVal);
01333 
01334   //  loop1MBB:
01335   //    ll      oldval,0(alginedaddr)
01336   //    and     maskedoldval0,oldval,mask
01337   //    bne     maskedoldval0,shiftedcmpval,sinkMBB
01338   BB = loop1MBB;
01339   BuildMI(BB, DL, TII->get(LL), OldVal).addReg(AlignedAddr).addImm(0);
01340   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
01341     .addReg(OldVal).addReg(Mask);
01342   BuildMI(BB, DL, TII->get(Mips::BNE))
01343     .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
01344 
01345   //  loop2MBB:
01346   //    and     maskedoldval1,oldval,mask2
01347   //    or      storeval,maskedoldval1,shiftednewval
01348   //    sc      success,storeval,0(alignedaddr)
01349   //    beq     success,$0,loop1MBB
01350   BB = loop2MBB;
01351   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
01352     .addReg(OldVal).addReg(Mask2);
01353   BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
01354     .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
01355   BuildMI(BB, DL, TII->get(SC), Success)
01356       .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
01357   BuildMI(BB, DL, TII->get(Mips::BEQ))
01358       .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
01359 
01360   //  sinkMBB:
01361   //    srl     srlres,maskedoldval0,shiftamt
01362   //    sll     sllres,srlres,24
01363   //    sra     dest,sllres,24
01364   BB = sinkMBB;
01365   int64_t ShiftImm = (Size == 1) ? 24 : 16;
01366 
01367   BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
01368       .addReg(ShiftAmt).addReg(MaskedOldVal0);
01369   BuildMI(BB, DL, TII->get(Mips::SLL), SllRes)
01370       .addReg(SrlRes).addImm(ShiftImm);
01371   BuildMI(BB, DL, TII->get(Mips::SRA), Dest)
01372       .addReg(SllRes).addImm(ShiftImm);
01373 
01374   MI->eraseFromParent();   // The instruction is gone now.
01375 
01376   return exitMBB;
01377 }
01378 
01379 //===----------------------------------------------------------------------===//
01380 //  Misc Lower Operation implementation
01381 //===----------------------------------------------------------------------===//
01382 SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
01383   SDValue Chain = Op.getOperand(0);
01384   SDValue Table = Op.getOperand(1);
01385   SDValue Index = Op.getOperand(2);
01386   DebugLoc DL = Op.getDebugLoc();
01387   EVT PTy = getPointerTy();
01388   unsigned EntrySize =
01389     DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
01390 
01391   Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
01392                       DAG.getConstant(EntrySize, PTy));
01393   SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
01394 
01395   EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
01396   Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
01397                         MachinePointerInfo::getJumpTable(), MemVT, false, false,
01398                         0);
01399   Chain = Addr.getValue(1);
01400 
01401   if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) || IsN64) {
01402     // For PIC, the sequence is:
01403     // BRIND(load(Jumptable + index) + RelocBase)
01404     // RelocBase can be JumpTable, GOT or some sort of global base.
01405     Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
01406                        getPICJumpTableRelocBase(Table, DAG));
01407   }
01408 
01409   return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
01410 }
01411 
01412 SDValue MipsTargetLowering::
01413 lowerBRCOND(SDValue Op, SelectionDAG &DAG) const
01414 {
01415   // The first operand is the chain, the second is the condition, the third is
01416   // the block to branch to if the condition is true.
01417   SDValue Chain = Op.getOperand(0);
01418   SDValue Dest = Op.getOperand(2);
01419   DebugLoc DL = Op.getDebugLoc();
01420 
01421   SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
01422 
01423   // Return if flag is not set by a floating point comparison.
01424   if (CondRes.getOpcode() != MipsISD::FPCmp)
01425     return Op;
01426 
01427   SDValue CCNode  = CondRes.getOperand(2);
01428   Mips::CondCode CC =
01429     (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
01430   unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
01431   SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
01432   return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
01433                      Dest, CondRes);
01434 }
01435 
01436 SDValue MipsTargetLowering::
01437 lowerSELECT(SDValue Op, SelectionDAG &DAG) const
01438 {
01439   SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
01440 
01441   // Return if flag is not set by a floating point comparison.
01442   if (Cond.getOpcode() != MipsISD::FPCmp)
01443     return Op;
01444 
01445   return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
01446                       Op.getDebugLoc());
01447 }
01448 
01449 SDValue MipsTargetLowering::
01450 lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
01451 {
01452   DebugLoc DL = Op.getDebugLoc();
01453   EVT Ty = Op.getOperand(0).getValueType();
01454   SDValue Cond = DAG.getNode(ISD::SETCC, DL,
01455                              getSetCCResultType(*DAG.getContext(), Ty),
01456                              Op.getOperand(0), Op.getOperand(1),
01457                              Op.getOperand(4));
01458 
01459   return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
01460                      Op.getOperand(3));
01461 }
01462 
01463 SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
01464   SDValue Cond = createFPCmp(DAG, Op);
01465 
01466   assert(Cond.getOpcode() == MipsISD::FPCmp &&
01467          "Floating point operand expected.");
01468 
01469   SDValue True  = DAG.getConstant(1, MVT::i32);
01470   SDValue False = DAG.getConstant(0, MVT::i32);
01471 
01472   return createCMovFP(DAG, Cond, True, False, Op.getDebugLoc());
01473 }
01474 
01475 SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
01476                                                SelectionDAG &DAG) const {
01477   // FIXME there isn't actually debug info here
01478   DebugLoc DL = Op.getDebugLoc();
01479   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
01480 
01481   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64) {
01482     const MipsTargetObjectFile &TLOF =
01483       (const MipsTargetObjectFile&)getObjFileLowering();
01484 
01485     // %gp_rel relocation
01486     if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
01487       SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
01488                                               MipsII::MO_GPREL);
01489       SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
01490                                       DAG.getVTList(MVT::i32), &GA, 1);
01491       SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
01492       return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
01493     }
01494 
01495     // %hi/%lo relocation
01496     return getAddrNonPIC(Op, DAG);
01497   }
01498 
01499   if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
01500     return getAddrLocal(Op, DAG, HasMips64);
01501 
01502   if (LargeGOT)
01503     return getAddrGlobalLargeGOT(Op, DAG, MipsII::MO_GOT_HI16,
01504                                  MipsII::MO_GOT_LO16);
01505 
01506   return getAddrGlobal(Op, DAG,
01507                        HasMips64 ? MipsII::MO_GOT_DISP : MipsII::MO_GOT16);
01508 }
01509 
01510 SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
01511                                               SelectionDAG &DAG) const {
01512   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
01513     return getAddrNonPIC(Op, DAG);
01514 
01515   return getAddrLocal(Op, DAG, HasMips64);
01516 }
01517 
01518 SDValue MipsTargetLowering::
01519 lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
01520 {
01521   // If the relocation model is PIC, use the General Dynamic TLS Model or
01522   // Local Dynamic TLS model, otherwise use the Initial Exec or
01523   // Local Exec TLS Model.
01524 
01525   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
01526   DebugLoc DL = GA->getDebugLoc();
01527   const GlobalValue *GV = GA->getGlobal();
01528   EVT PtrVT = getPointerTy();
01529 
01530   TLSModel::Model model = getTargetMachine().getTLSModel(GV);
01531 
01532   if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
01533     // General Dynamic and Local Dynamic TLS Model.
01534     unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
01535                                                       : MipsII::MO_TLSGD;
01536 
01537     SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
01538     SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
01539                                    getGlobalReg(DAG, PtrVT), TGA);
01540     unsigned PtrSize = PtrVT.getSizeInBits();
01541     IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
01542 
01543     SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
01544 
01545     ArgListTy Args;
01546     ArgListEntry Entry;
01547     Entry.Node = Argument;
01548     Entry.Ty = PtrTy;
01549     Args.push_back(Entry);
01550 
01551     TargetLowering::CallLoweringInfo CLI(DAG.getEntryNode(), PtrTy,
01552                   false, false, false, false, 0, CallingConv::C,
01553                   /*IsTailCall=*/false, /*doesNotRet=*/false,
01554                   /*isReturnValueUsed=*/true,
01555                   TlsGetAddr, Args, DAG, DL);
01556     std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
01557 
01558     SDValue Ret = CallResult.first;
01559 
01560     if (model != TLSModel::LocalDynamic)
01561       return Ret;
01562 
01563     SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
01564                                                MipsII::MO_DTPREL_HI);
01565     SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
01566     SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
01567                                                MipsII::MO_DTPREL_LO);
01568     SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
01569     SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
01570     return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
01571   }
01572 
01573   SDValue Offset;
01574   if (model == TLSModel::InitialExec) {
01575     // Initial Exec TLS Model
01576     SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
01577                                              MipsII::MO_GOTTPREL);
01578     TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
01579                       TGA);
01580     Offset = DAG.getLoad(PtrVT, DL,
01581                          DAG.getEntryNode(), TGA, MachinePointerInfo(),
01582                          false, false, false, 0);
01583   } else {
01584     // Local Exec TLS Model
01585     assert(model == TLSModel::LocalExec);
01586     SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
01587                                                MipsII::MO_TPREL_HI);
01588     SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
01589                                                MipsII::MO_TPREL_LO);
01590     SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
01591     SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
01592     Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
01593   }
01594 
01595   SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
01596   return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
01597 }
01598 
01599 SDValue MipsTargetLowering::
01600 lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
01601 {
01602   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
01603     return getAddrNonPIC(Op, DAG);
01604 
01605   return getAddrLocal(Op, DAG, HasMips64);
01606 }
01607 
01608 SDValue MipsTargetLowering::
01609 lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
01610 {
01611   // gp_rel relocation
01612   // FIXME: we should reference the constant pool using small data sections,
01613   // but the asm printer currently doesn't support this feature without
01614   // hacking it. This feature should come soon so we can uncomment the
01615   // stuff below.
01616   //if (IsInSmallSection(C->getType())) {
01617   //  SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
01618   //  SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
01619   //  ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
01620 
01621   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ && !IsN64)
01622     return getAddrNonPIC(Op, DAG);
01623 
01624   return getAddrLocal(Op, DAG, HasMips64);
01625 }
01626 
01627 SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
01628   MachineFunction &MF = DAG.getMachineFunction();
01629   MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
01630 
01631   DebugLoc DL = Op.getDebugLoc();
01632   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
01633                                  getPointerTy());
01634 
01635   // vastart just stores the address of the VarArgsFrameIndex slot into the
01636   // memory location argument.
01637   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
01638   return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
01639                       MachinePointerInfo(SV), false, false, 0);
01640 }
01641 
01642 static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
01643   EVT TyX = Op.getOperand(0).getValueType();
01644   EVT TyY = Op.getOperand(1).getValueType();
01645   SDValue Const1 = DAG.getConstant(1, MVT::i32);
01646   SDValue Const31 = DAG.getConstant(31, MVT::i32);
01647   DebugLoc DL = Op.getDebugLoc();
01648   SDValue Res;
01649 
01650   // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
01651   // to i32.
01652   SDValue X = (TyX == MVT::f32) ?
01653     DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
01654     DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
01655                 Const1);
01656   SDValue Y = (TyY == MVT::f32) ?
01657     DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
01658     DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
01659                 Const1);
01660 
01661   if (HasR2) {
01662     // ext  E, Y, 31, 1  ; extract bit31 of Y
01663     // ins  X, E, 31, 1  ; insert extracted bit at bit31 of X
01664     SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
01665     Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
01666   } else {
01667     // sll SllX, X, 1
01668     // srl SrlX, SllX, 1
01669     // srl SrlY, Y, 31
01670     // sll SllY, SrlX, 31
01671     // or  Or, SrlX, SllY
01672     SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
01673     SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
01674     SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
01675     SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
01676     Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
01677   }
01678 
01679   if (TyX == MVT::f32)
01680     return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
01681 
01682   SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
01683                              Op.getOperand(0), DAG.getConstant(0, MVT::i32));
01684   return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
01685 }
01686 
01687 static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
01688   unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
01689   unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
01690   EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
01691   SDValue Const1 = DAG.getConstant(1, MVT::i32);
01692   DebugLoc DL = Op.getDebugLoc();
01693 
01694   // Bitcast to integer nodes.
01695   SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
01696   SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
01697 
01698   if (HasR2) {
01699     // ext  E, Y, width(Y) - 1, 1  ; extract bit width(Y)-1 of Y
01700     // ins  X, E, width(X) - 1, 1  ; insert extracted bit at bit width(X)-1 of X
01701     SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
01702                             DAG.getConstant(WidthY - 1, MVT::i32), Const1);
01703 
01704     if (WidthX > WidthY)
01705       E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
01706     else if (WidthY > WidthX)
01707       E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
01708 
01709     SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
01710                             DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
01711     return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
01712   }
01713 
01714   // (d)sll SllX, X, 1
01715   // (d)srl SrlX, SllX, 1
01716   // (d)srl SrlY, Y, width(Y)-1
01717   // (d)sll SllY, SrlX, width(Y)-1
01718   // or     Or, SrlX, SllY
01719   SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
01720   SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
01721   SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
01722                              DAG.getConstant(WidthY - 1, MVT::i32));
01723 
01724   if (WidthX > WidthY)
01725     SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
01726   else if (WidthY > WidthX)
01727     SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
01728 
01729   SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
01730                              DAG.getConstant(WidthX - 1, MVT::i32));
01731   SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
01732   return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
01733 }
01734 
01735 SDValue
01736 MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
01737   if (Subtarget->hasMips64())
01738     return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasMips32r2());
01739 
01740   return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasMips32r2());
01741 }
01742 
01743 static SDValue lowerFABS32(SDValue Op, SelectionDAG &DAG, bool HasR2) {
01744   SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
01745   DebugLoc DL = Op.getDebugLoc();
01746 
01747   // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
01748   // to i32.
01749   SDValue X = (Op.getValueType() == MVT::f32) ?
01750     DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
01751     DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
01752                 Const1);
01753 
01754   // Clear MSB.
01755   if (HasR2)
01756     Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32,
01757                       DAG.getRegister(Mips::ZERO, MVT::i32),
01758                       DAG.getConstant(31, MVT::i32), Const1, X);
01759   else {
01760     SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
01761     Res = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
01762   }
01763 
01764   if (Op.getValueType() == MVT::f32)
01765     return DAG.getNode(ISD::BITCAST, DL, MVT::f32, Res);
01766 
01767   SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
01768                              Op.getOperand(0), DAG.getConstant(0, MVT::i32));
01769   return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
01770 }
01771 
01772 static SDValue lowerFABS64(SDValue Op, SelectionDAG &DAG, bool HasR2) {
01773   SDValue Res, Const1 = DAG.getConstant(1, MVT::i32);
01774   DebugLoc DL = Op.getDebugLoc();
01775 
01776   // Bitcast to integer node.
01777   SDValue X = DAG.getNode(ISD::BITCAST, DL, MVT::i64, Op.getOperand(0));
01778 
01779   // Clear MSB.
01780   if (HasR2)
01781     Res = DAG.getNode(MipsISD::Ins, DL, MVT::i64,
01782                       DAG.getRegister(Mips::ZERO_64, MVT::i64),
01783                       DAG.getConstant(63, MVT::i32), Const1, X);
01784   else {
01785     SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i64, X, Const1);
01786     Res = DAG.getNode(ISD::SRL, DL, MVT::i64, SllX, Const1);
01787   }
01788 
01789   return DAG.getNode(ISD::BITCAST, DL, MVT::f64, Res);
01790 }
01791 
01792 SDValue
01793 MipsTargetLowering::lowerFABS(SDValue Op, SelectionDAG &DAG) const {
01794   if (Subtarget->hasMips64() && (Op.getValueType() == MVT::f64))
01795     return lowerFABS64(Op, DAG, Subtarget->hasMips32r2());
01796 
01797   return lowerFABS32(Op, DAG, Subtarget->hasMips32r2());
01798 }
01799 
01800 SDValue MipsTargetLowering::
01801 lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
01802   // check the depth
01803   assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
01804          "Frame address can only be determined for current frame.");
01805 
01806   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
01807   MFI->setFrameAddressIsTaken(true);
01808   EVT VT = Op.getValueType();
01809   DebugLoc DL = Op.getDebugLoc();
01810   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), DL,
01811                                          IsN64 ? Mips::FP_64 : Mips::FP, VT);
01812   return FrameAddr;
01813 }
01814 
01815 SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
01816                                             SelectionDAG &DAG) const {
01817   // check the depth
01818   assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
01819          "Return address can be determined only for current frame.");
01820 
01821   MachineFunction &MF = DAG.getMachineFunction();
01822   MachineFrameInfo *MFI = MF.getFrameInfo();
01823   MVT VT = Op.getSimpleValueType();
01824   unsigned RA = IsN64 ? Mips::RA_64 : Mips::RA;
01825   MFI->setReturnAddressIsTaken(true);
01826 
01827   // Return RA, which contains the return address. Mark it an implicit live-in.
01828   unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
01829   return DAG.getCopyFromReg(DAG.getEntryNode(), Op.getDebugLoc(), Reg, VT);
01830 }
01831 
01832 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is
01833 // generated from __builtin_eh_return (offset, handler)
01834 // The effect of this is to adjust the stack pointer by "offset"
01835 // and then branch to "handler".
01836 SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
01837                                                                      const {
01838   MachineFunction &MF = DAG.getMachineFunction();
01839   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
01840 
01841   MipsFI->setCallsEhReturn();
01842   SDValue Chain     = Op.getOperand(0);
01843   SDValue Offset    = Op.getOperand(1);
01844   SDValue Handler   = Op.getOperand(2);
01845   DebugLoc DL       = Op.getDebugLoc();
01846   EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
01847 
01848   // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
01849   // EH_RETURN nodes, so that instructions are emitted back-to-back.
01850   unsigned OffsetReg = IsN64 ? Mips::V1_64 : Mips::V1;
01851   unsigned AddrReg = IsN64 ? Mips::V0_64 : Mips::V0;
01852   Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
01853   Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
01854   return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
01855                      DAG.getRegister(OffsetReg, Ty),
01856                      DAG.getRegister(AddrReg, getPointerTy()),
01857                      Chain.getValue(1));
01858 }
01859 
01860 SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
01861                                               SelectionDAG &DAG) const {
01862   // FIXME: Need pseudo-fence for 'singlethread' fences
01863   // FIXME: Set SType for weaker fences where supported/appropriate.
01864   unsigned SType = 0;
01865   DebugLoc DL = Op.getDebugLoc();
01866   return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
01867                      DAG.getConstant(SType, MVT::i32));
01868 }
01869 
01870 SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
01871                                                 SelectionDAG &DAG) const {
01872   DebugLoc DL = Op.getDebugLoc();
01873   SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
01874   SDValue Shamt = Op.getOperand(2);
01875 
01876   // if shamt < 32:
01877   //  lo = (shl lo, shamt)
01878   //  hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
01879   // else:
01880   //  lo = 0
01881   //  hi = (shl lo, shamt[4:0])
01882   SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
01883                             DAG.getConstant(-1, MVT::i32));
01884   SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
01885                                       DAG.getConstant(1, MVT::i32));
01886   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
01887                                      Not);
01888   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
01889   SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
01890   SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
01891   SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
01892                              DAG.getConstant(0x20, MVT::i32));
01893   Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
01894                    DAG.getConstant(0, MVT::i32), ShiftLeftLo);
01895   Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
01896 
01897   SDValue Ops[2] = {Lo, Hi};
01898   return DAG.getMergeValues(Ops, 2, DL);
01899 }
01900 
01901 SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
01902                                                  bool IsSRA) const {
01903   DebugLoc DL = Op.getDebugLoc();
01904   SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
01905   SDValue Shamt = Op.getOperand(2);
01906 
01907   // if shamt < 32:
01908   //  lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
01909   //  if isSRA:
01910   //    hi = (sra hi, shamt)
01911   //  else:
01912   //    hi = (srl hi, shamt)
01913   // else:
01914   //  if isSRA:
01915   //   lo = (sra hi, shamt[4:0])
01916   //   hi = (sra hi, 31)
01917   //  else:
01918   //   lo = (srl hi, shamt[4:0])
01919   //   hi = 0
01920   SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
01921                             DAG.getConstant(-1, MVT::i32));
01922   SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
01923                                      DAG.getConstant(1, MVT::i32));
01924   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
01925   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
01926   SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
01927   SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
01928                                      Hi, Shamt);
01929   SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
01930                              DAG.getConstant(0x20, MVT::i32));
01931   SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
01932                                 DAG.getConstant(31, MVT::i32));
01933   Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
01934   Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
01935                    IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
01936                    ShiftRightHi);
01937 
01938   SDValue Ops[2] = {Lo, Hi};
01939   return DAG.getMergeValues(Ops, 2, DL);
01940 }
01941 
01942 static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
01943                             SDValue Chain, SDValue Src, unsigned Offset) {
01944   SDValue Ptr = LD->getBasePtr();
01945   EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
01946   EVT BasePtrVT = Ptr.getValueType();
01947   DebugLoc DL = LD->getDebugLoc();
01948   SDVTList VTList = DAG.getVTList(VT, MVT::Other);
01949 
01950   if (Offset)
01951     Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
01952                       DAG.getConstant(Offset, BasePtrVT));
01953 
01954   SDValue Ops[] = { Chain, Ptr, Src };
01955   return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
01956                                  LD->getMemOperand());
01957 }
01958 
01959 // Expand an unaligned 32 or 64-bit integer load node.
01960 SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
01961   LoadSDNode *LD = cast<LoadSDNode>(Op);
01962   EVT MemVT = LD->getMemoryVT();
01963 
01964   // Return if load is aligned or if MemVT is neither i32 nor i64.
01965   if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
01966       ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
01967     return SDValue();
01968 
01969   bool IsLittle = Subtarget->isLittle();
01970   EVT VT = Op.getValueType();
01971   ISD::LoadExtType ExtType = LD->getExtensionType();
01972   SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
01973 
01974   assert((VT == MVT::i32) || (VT == MVT::i64));
01975 
01976   // Expand
01977   //  (set dst, (i64 (load baseptr)))
01978   // to
01979   //  (set tmp, (ldl (add baseptr, 7), undef))
01980   //  (set dst, (ldr baseptr, tmp))
01981   if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
01982     SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
01983                                IsLittle ? 7 : 0);
01984     return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
01985                         IsLittle ? 0 : 7);
01986   }
01987 
01988   SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
01989                              IsLittle ? 3 : 0);
01990   SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
01991                              IsLittle ? 0 : 3);
01992 
01993   // Expand
01994   //  (set dst, (i32 (load baseptr))) or
01995   //  (set dst, (i64 (sextload baseptr))) or
01996   //  (set dst, (i64 (extload baseptr)))
01997   // to
01998   //  (set tmp, (lwl (add baseptr, 3), undef))
01999   //  (set dst, (lwr baseptr, tmp))
02000   if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
02001       (ExtType == ISD::EXTLOAD))
02002     return LWR;
02003 
02004   assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
02005 
02006   // Expand
02007   //  (set dst, (i64 (zextload baseptr)))
02008   // to
02009   //  (set tmp0, (lwl (add baseptr, 3), undef))
02010   //  (set tmp1, (lwr baseptr, tmp0))
02011   //  (set tmp2, (shl tmp1, 32))
02012   //  (set dst, (srl tmp2, 32))
02013   DebugLoc DL = LD->getDebugLoc();
02014   SDValue Const32 = DAG.getConstant(32, MVT::i32);
02015   SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
02016   SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
02017   SDValue Ops[] = { SRL, LWR.getValue(1) };
02018   return DAG.getMergeValues(Ops, 2, DL);
02019 }
02020 
02021 static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
02022                              SDValue Chain, unsigned Offset) {
02023   SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
02024   EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
02025   DebugLoc DL = SD->getDebugLoc();
02026   SDVTList VTList = DAG.getVTList(MVT::Other);
02027 
02028   if (Offset)
02029     Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
02030                       DAG.getConstant(Offset, BasePtrVT));
02031 
02032   SDValue Ops[] = { Chain, Value, Ptr };
02033   return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, 3, MemVT,
02034                                  SD->getMemOperand());
02035 }
02036 
02037 // Expand an unaligned 32 or 64-bit integer store node.
02038 static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
02039                                       bool IsLittle) {
02040   SDValue Value = SD->getValue(), Chain = SD->getChain();
02041   EVT VT = Value.getValueType();
02042 
02043   // Expand
02044   //  (store val, baseptr) or
02045   //  (truncstore val, baseptr)
02046   // to
02047   //  (swl val, (add baseptr, 3))
02048   //  (swr val, baseptr)
02049   if ((VT == MVT::i32) || SD->isTruncatingStore()) {
02050     SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
02051                                 IsLittle ? 3 : 0);
02052     return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
02053   }
02054 
02055   assert(VT == MVT::i64);
02056 
02057   // Expand
02058   //  (store val, baseptr)
02059   // to
02060   //  (sdl val, (add baseptr, 7))
02061   //  (sdr val, baseptr)
02062   SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
02063   return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
02064 }
02065 
02066 // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
02067 static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
02068   SDValue Val = SD->getValue();
02069 
02070   if (Val.getOpcode() != ISD::FP_TO_SINT)
02071     return SDValue();
02072 
02073   EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
02074   SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, Val.getDebugLoc(), FPTy,
02075                            Val.getOperand(0));
02076 
02077   return DAG.getStore(SD->getChain(), SD->getDebugLoc(), Tr, SD->getBasePtr(),
02078                       SD->getPointerInfo(), SD->isVolatile(),
02079                       SD->isNonTemporal(), SD->getAlignment());
02080 }
02081 
02082 SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
02083   StoreSDNode *SD = cast<StoreSDNode>(Op);
02084   EVT MemVT = SD->getMemoryVT();
02085 
02086   // Lower unaligned integer stores.
02087   if ((SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
02088       ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
02089     return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
02090 
02091   return lowerFP_TO_SINT_STORE(SD, DAG);
02092 }
02093 
02094 SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
02095   if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
02096       || cast<ConstantSDNode>
02097         (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
02098       || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
02099     return SDValue();
02100 
02101   // The pattern
02102   //   (add (frameaddr 0), (frame_to_args_offset))
02103   // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
02104   //   (add FrameObject, 0)
02105   // where FrameObject is a fixed StackObject with offset 0 which points to
02106   // the old stack pointer.
02107   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
02108   EVT ValTy = Op->getValueType(0);
02109   int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
02110   SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
02111   return DAG.getNode(ISD::ADD, Op->getDebugLoc(), ValTy, InArgsAddr,
02112                      DAG.getConstant(0, ValTy));
02113 }
02114 
02115 SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
02116                                             SelectionDAG &DAG) const {
02117   EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
02118   SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, Op.getDebugLoc(), FPTy,
02119                               Op.getOperand(0));
02120   return DAG.getNode(ISD::BITCAST, Op.getDebugLoc(), Op.getValueType(), Trunc);
02121 }
02122 
02123 //===----------------------------------------------------------------------===//
02124 //                      Calling Convention Implementation
02125 //===----------------------------------------------------------------------===//
02126 
02127 //===----------------------------------------------------------------------===//
02128 // TODO: Implement a generic logic using tblgen that can support this.
02129 // Mips O32 ABI rules:
02130 // ---
02131 // i32 - Passed in A0, A1, A2, A3 and stack
02132 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
02133 //       an argument. Otherwise, passed in A1, A2, A3 and stack.
02134 // f64 - Only passed in two aliased f32 registers if no int reg has been used
02135 //       yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
02136 //       not used, it must be shadowed. If only A3 is avaiable, shadow it and
02137 //       go to stack.
02138 //
02139 //  For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
02140 //===----------------------------------------------------------------------===//
02141 
02142 static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
02143                        MVT LocVT, CCValAssign::LocInfo LocInfo,
02144                        ISD::ArgFlagsTy ArgFlags, CCState &State) {
02145 
02146   static const unsigned IntRegsSize=4, FloatRegsSize=2;
02147 
02148   static const uint16_t IntRegs[] = {
02149       Mips::A0, Mips::A1, Mips::A2, Mips::A3
02150   };
02151   static const uint16_t F32Regs[] = {
02152       Mips::F12, Mips::F14
02153   };
02154   static const uint16_t F64Regs[] = {
02155       Mips::D6, Mips::D7
02156   };
02157 
02158   // Do not process byval args here.
02159   if (ArgFlags.isByVal())
02160     return true;
02161 
02162   // Promote i8 and i16
02163   if (LocVT == MVT::i8 || LocVT == MVT::i16) {
02164     LocVT = MVT::i32;
02165     if (ArgFlags.isSExt())
02166       LocInfo = CCValAssign::SExt;
02167     else if (ArgFlags.isZExt())
02168       LocInfo = CCValAssign::ZExt;
02169     else
02170       LocInfo = CCValAssign::AExt;
02171   }
02172 
02173   unsigned Reg;
02174 
02175   // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
02176   // is true: function is vararg, argument is 3rd or higher, there is previous
02177   // argument which is not f32 or f64.
02178   bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
02179       || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
02180   unsigned OrigAlign = ArgFlags.getOrigAlign();
02181   bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
02182 
02183   if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
02184     Reg = State.AllocateReg(IntRegs, IntRegsSize);
02185     // If this is the first part of an i64 arg,
02186     // the allocated register must be either A0 or A2.
02187     if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
02188       Reg = State.AllocateReg(IntRegs, IntRegsSize);
02189     LocVT = MVT::i32;
02190   } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
02191     // Allocate int register and shadow next int register. If first
02192     // available register is Mips::A1 or Mips::A3, shadow it too.
02193     Reg = State.AllocateReg(IntRegs, IntRegsSize);
02194     if (Reg == Mips::A1 || Reg == Mips::A3)
02195       Reg = State.AllocateReg(IntRegs, IntRegsSize);
02196     State.AllocateReg(IntRegs, IntRegsSize);
02197     LocVT = MVT::i32;
02198   } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
02199     // we are guaranteed to find an available float register
02200     if (ValVT == MVT::f32) {
02201       Reg = State.AllocateReg(F32Regs, FloatRegsSize);
02202       // Shadow int register
02203       State.AllocateReg(IntRegs, IntRegsSize);
02204     } else {
02205       Reg = State.AllocateReg(F64Regs, FloatRegsSize);
02206       // Shadow int registers
02207       unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
02208       if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
02209         State.AllocateReg(IntRegs, IntRegsSize);
02210       State.AllocateReg(IntRegs, IntRegsSize);
02211     }
02212   } else
02213     llvm_unreachable("Cannot handle this ValVT.");
02214 
02215   if (!Reg) {
02216     unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
02217                                           OrigAlign);
02218     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
02219   } else
02220     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
02221 
02222   return false;
02223 }
02224 
02225 #include "MipsGenCallingConv.inc"
02226 
02227 //===----------------------------------------------------------------------===//
02228 //                  Call Calling Convention Implementation
02229 //===----------------------------------------------------------------------===//
02230 
02231 static const unsigned O32IntRegsSize = 4;
02232 
02233 // Return next O32 integer argument register.
02234 static unsigned getNextIntArgReg(unsigned Reg) {
02235   assert((Reg == Mips::A0) || (Reg == Mips::A2));
02236   return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
02237 }
02238 
02239 SDValue
02240 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
02241                                    SDValue Chain, SDValue Arg, DebugLoc DL,
02242                                    bool IsTailCall, SelectionDAG &DAG) const {
02243   if (!IsTailCall) {
02244     SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
02245                                  DAG.getIntPtrConstant(Offset));
02246     return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
02247                         false, 0);
02248   }
02249 
02250   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
02251   int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
02252   SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
02253   return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
02254                       /*isVolatile=*/ true, false, 0);
02255 }
02256 
02257 void MipsTargetLowering::
02258 getOpndList(SmallVectorImpl<SDValue> &Ops,
02259             std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
02260             bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
02261             CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
02262   // Insert node "GP copy globalreg" before call to function.
02263   //
02264   // R_MIPS_CALL* operators (emitted when non-internal functions are called
02265   // in PIC mode) allow symbols to be resolved via lazy binding.
02266   // The lazy binding stub requires GP to point to the GOT.
02267   if (IsPICCall && !InternalLinkage) {
02268     unsigned GPReg = IsN64 ? Mips::GP_64 : Mips::GP;
02269     EVT Ty = IsN64 ? MVT::i64 : MVT::i32;
02270     RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
02271   }
02272 
02273   // Build a sequence of copy-to-reg nodes chained together with token
02274   // chain and flag operands which copy the outgoing args into registers.
02275   // The InFlag in necessary since all emitted instructions must be
02276   // stuck together.
02277   SDValue InFlag;
02278 
02279   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
02280     Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
02281                                  RegsToPass[i].second, InFlag);
02282     InFlag = Chain.getValue(1);
02283   }
02284 
02285   // Add argument registers to the end of the list so that they are
02286   // known live into the call.
02287   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
02288     Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
02289                                       RegsToPass[i].second.getValueType()));
02290 
02291   // Add a register mask operand representing the call-preserved registers.
02292   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
02293   const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
02294   assert(Mask && "Missing call preserved mask for calling convention");
02295   if (Subtarget->inMips16HardFloat()) {
02296     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
02297       llvm::StringRef Sym = G->getGlobal()->getName();
02298       Function *F = G->getGlobal()->getParent()->getFunction(Sym);
02299       if (F->hasFnAttribute("__Mips16RetHelper")) {
02300         Mask = MipsRegisterInfo::getMips16RetHelperMask();
02301       }
02302     }
02303   }
02304   Ops.push_back(CLI.DAG.getRegisterMask(Mask));
02305 
02306   if (InFlag.getNode())
02307     Ops.push_back(InFlag);
02308 }
02309 
02310 /// LowerCall - functions arguments are copied from virtual regs to
02311 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
02312 SDValue
02313 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
02314                               SmallVectorImpl<SDValue> &InVals) const {
02315   SelectionDAG &DAG                     = CLI.DAG;
02316   DebugLoc &DL                          = CLI.DL;
02317   SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
02318   SmallVector<SDValue, 32> &OutVals     = CLI.OutVals;
02319   SmallVector<ISD::InputArg, 32> &Ins   = CLI.Ins;
02320   SDValue Chain                         = CLI.Chain;
02321   SDValue Callee                        = CLI.Callee;
02322   bool &IsTailCall                      = CLI.IsTailCall;
02323   CallingConv::ID CallConv              = CLI.CallConv;
02324   bool IsVarArg                         = CLI.IsVarArg;
02325 
02326   MachineFunction &MF = DAG.getMachineFunction();
02327   MachineFrameInfo *MFI = MF.getFrameInfo();
02328   const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
02329   bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
02330 
02331   // Analyze operands of the call, assigning locations to each operand.
02332   SmallVector<CCValAssign, 16> ArgLocs;
02333   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
02334                  getTargetMachine(), ArgLocs, *DAG.getContext());
02335   MipsCC::SpecialCallingConvType SpecialCallingConv =
02336     getSpecialCallingConv(Callee);
02337   MipsCC MipsCCInfo(CallConv, IsO32, CCInfo, SpecialCallingConv);
02338 
02339   MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
02340                                  getTargetMachine().Options.UseSoftFloat,
02341                                  Callee.getNode(), CLI.Args);
02342 
02343   // Get a count of how many bytes are to be pushed on the stack.
02344   unsigned NextStackOffset = CCInfo.getNextStackOffset();
02345 
02346   // Check if it's really possible to do a tail call.
02347   if (IsTailCall)
02348     IsTailCall =
02349       isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
02350                                         *MF.getInfo<MipsFunctionInfo>());
02351 
02352   if (IsTailCall)
02353     ++NumTailCalls;
02354 
02355   // Chain is the output chain of the last Load/Store or CopyToReg node.
02356   // ByValChain is the output chain of the last Memcpy node created for copying
02357   // byval arguments to the stack.
02358   unsigned StackAlignment = TFL->getStackAlignment();
02359   NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
02360   SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
02361 
02362   if (!IsTailCall)
02363     Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal);
02364 
02365   SDValue StackPtr = DAG.getCopyFromReg(Chain, DL,
02366                                         IsN64 ? Mips::SP_64 : Mips::SP,
02367                                         getPointerTy());
02368 
02369   // With EABI is it possible to have 16 args on registers.
02370   std::deque< std::pair<unsigned, SDValue> > RegsToPass;
02371   SmallVector<SDValue, 8> MemOpChains;
02372   MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
02373 
02374   // Walk the register/memloc assignments, inserting copies/loads.
02375   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
02376     SDValue Arg = OutVals[i];
02377     CCValAssign &VA = ArgLocs[i];
02378     MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
02379     ISD::ArgFlagsTy Flags = Outs[i].Flags;
02380 
02381     // ByVal Arg.
02382     if (Flags.isByVal()) {
02383       assert(Flags.getByValSize() &&
02384              "ByVal args of size 0 should have been ignored by front-end.");
02385       assert(ByValArg != MipsCCInfo.byval_end());
02386       assert(!IsTailCall &&
02387              "Do not tail-call optimize if there is a byval argument.");
02388       passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
02389                    MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
02390       ++ByValArg;
02391       continue;
02392     }
02393 
02394     // Promote the value if needed.
02395     switch (VA.getLocInfo()) {
02396     default: llvm_unreachable("Unknown loc info!");
02397     case CCValAssign::Full:
02398       if (VA.isRegLoc()) {
02399         if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
02400             (ValVT == MVT::f64 && LocVT == MVT::i64) ||
02401             (ValVT == MVT::i64 && LocVT == MVT::f64))
02402           Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
02403         else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
02404           SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
02405                                    Arg, DAG.getConstant(0, MVT::i32));
02406           SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
02407                                    Arg, DAG.getConstant(1, MVT::i32));
02408           if (!Subtarget->isLittle())
02409             std::swap(Lo, Hi);
02410           unsigned LocRegLo = VA.getLocReg();
02411           unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
02412           RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
02413           RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
02414           continue;
02415         }
02416       }
02417       break;
02418     case CCValAssign::SExt:
02419       Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
02420       break;
02421     case CCValAssign::ZExt:
02422       Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
02423       break;
02424     case CCValAssign::AExt:
02425       Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
02426       break;
02427     }
02428 
02429     // Arguments that can be passed on register must be kept at
02430     // RegsToPass vector
02431     if (VA.isRegLoc()) {
02432       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
02433       continue;
02434     }
02435 
02436     // Register can't get to this point...
02437     assert(VA.isMemLoc());
02438 
02439     // emit ISD::STORE whichs stores the
02440     // parameter value to a stack Location
02441     MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
02442                                          Chain, Arg, DL, IsTailCall, DAG));
02443   }
02444 
02445   // Transform all store nodes into one single node because all store
02446   // nodes are independent of each other.
02447   if (!MemOpChains.empty())
02448     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
02449                         &MemOpChains[0], MemOpChains.size());
02450 
02451   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
02452   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
02453   // node so that legalize doesn't hack it.
02454   bool IsPICCall = (IsN64 || IsPIC); // true if calls are translated to jalr $25
02455   bool GlobalOrExternal = false, InternalLinkage = false;
02456   SDValue CalleeLo;
02457 
02458   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
02459     if (IsPICCall) {
02460       InternalLinkage = G->getGlobal()->hasInternalLinkage();
02461 
02462       if (InternalLinkage)
02463         Callee = getAddrLocal(Callee, DAG, HasMips64);
02464       else if (LargeGOT)
02465         Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
02466                                        MipsII::MO_CALL_LO16);
02467       else
02468         Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
02469     } else
02470       Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
02471                                           MipsII::MO_NO_FLAG);
02472     GlobalOrExternal = true;
02473   }
02474   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
02475     if (!IsN64 && !IsPIC) // !N64 && static
02476       Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
02477                                             MipsII::MO_NO_FLAG);
02478     else if (LargeGOT)
02479       Callee = getAddrGlobalLargeGOT(Callee, DAG, MipsII::MO_CALL_HI16,
02480                                      MipsII::MO_CALL_LO16);
02481     else // N64 || PIC
02482       Callee = getAddrGlobal(Callee, DAG, MipsII::MO_GOT_CALL);
02483 
02484     GlobalOrExternal = true;
02485   }
02486 
02487   SmallVector<SDValue, 8> Ops(1, Chain);
02488   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
02489 
02490   getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
02491               CLI, Callee, Chain);
02492 
02493   if (IsTailCall)
02494     return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, &Ops[0], Ops.size());
02495 
02496   Chain  = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, &Ops[0], Ops.size());
02497   SDValue InFlag = Chain.getValue(1);
02498 
02499   // Create the CALLSEQ_END node.
02500   Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
02501                              DAG.getIntPtrConstant(0, true), InFlag);
02502   InFlag = Chain.getValue(1);
02503 
02504   // Handle result values, copying them out of physregs into vregs that we
02505   // return.
02506   return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
02507                          Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
02508 }
02509 
02510 /// LowerCallResult - Lower the result values of a call into the
02511 /// appropriate copies out of appropriate physical registers.
02512 SDValue
02513 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
02514                                     CallingConv::ID CallConv, bool IsVarArg,
02515                                     const SmallVectorImpl<ISD::InputArg> &Ins,
02516                                     DebugLoc DL, SelectionDAG &DAG,
02517                                     SmallVectorImpl<SDValue> &InVals,
02518                                     const SDNode *CallNode,
02519                                     const Type *RetTy) const {
02520   // Assign locations to each value returned by this call.
02521   SmallVector<CCValAssign, 16> RVLocs;
02522   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
02523                  getTargetMachine(), RVLocs, *DAG.getContext());
02524   MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
02525 
02526   MipsCCInfo.analyzeCallResult(Ins, getTargetMachine().Options.UseSoftFloat,
02527                                CallNode, RetTy);
02528 
02529   // Copy all of the result registers out of their specified physreg.
02530   for (unsigned i = 0; i != RVLocs.size(); ++i) {
02531     SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
02532                                      RVLocs[i].getLocVT(), InFlag);
02533     Chain = Val.getValue(1);
02534     InFlag = Val.getValue(2);
02535 
02536     if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
02537       Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
02538 
02539     InVals.push_back(Val);
02540   }
02541 
02542   return Chain;
02543 }
02544 
02545 //===----------------------------------------------------------------------===//
02546 //             Formal Arguments Calling Convention Implementation
02547 //===----------------------------------------------------------------------===//
02548 /// LowerFormalArguments - transform physical registers into virtual registers
02549 /// and generate load operations for arguments places on the stack.
02550 SDValue
02551 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
02552                                          CallingConv::ID CallConv,
02553                                          bool IsVarArg,
02554                                       const SmallVectorImpl<ISD::InputArg> &Ins,
02555                                          DebugLoc DL, SelectionDAG &DAG,
02556                                          SmallVectorImpl<SDValue> &InVals)
02557                                           const {
02558   MachineFunction &MF = DAG.getMachineFunction();
02559   MachineFrameInfo *MFI = MF.getFrameInfo();
02560   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
02561 
02562   MipsFI->setVarArgsFrameIndex(0);
02563 
02564   // Used with vargs to acumulate store chains.
02565   std::vector<SDValue> OutChains;
02566 
02567   // Assign locations to all of the incoming arguments.
02568   SmallVector<CCValAssign, 16> ArgLocs;
02569   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
02570                  getTargetMachine(), ArgLocs, *DAG.getContext());
02571   MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
02572   Function::const_arg_iterator FuncArg =
02573     DAG.getMachineFunction().getFunction()->arg_begin();
02574   bool UseSoftFloat = getTargetMachine().Options.UseSoftFloat;
02575 
02576   MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
02577   MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
02578                            MipsCCInfo.hasByValArg());
02579 
02580   unsigned CurArgIdx = 0;
02581   MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
02582 
02583   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
02584     CCValAssign &VA = ArgLocs[i];
02585     std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
02586     CurArgIdx = Ins[i].OrigArgIndex;
02587     EVT ValVT = VA.getValVT();
02588     ISD::ArgFlagsTy Flags = Ins[i].Flags;
02589     bool IsRegLoc = VA.isRegLoc();
02590 
02591     if (Flags.isByVal()) {
02592       assert(Flags.getByValSize() &&
02593              "ByVal args of size 0 should have been ignored by front-end.");
02594       assert(ByValArg != MipsCCInfo.byval_end());
02595       copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
02596                     MipsCCInfo, *ByValArg);
02597       ++ByValArg;
02598       continue;
02599     }
02600 
02601     // Arguments stored on registers
02602     if (IsRegLoc) {
02603       EVT RegVT = VA.getLocVT();
02604       unsigned ArgReg = VA.getLocReg();
02605       const TargetRegisterClass *RC;
02606 
02607       if (RegVT == MVT::i32)
02608         RC = Subtarget->inMips16Mode()? &Mips::CPU16RegsRegClass :
02609                                         &Mips::CPURegsRegClass;
02610       else if (RegVT == MVT::i64)
02611         RC = &Mips::CPU64RegsRegClass;
02612       else if (RegVT == MVT::f32)
02613         RC = &Mips::FGR32RegClass;
02614       else if (RegVT == MVT::f64)
02615         RC = HasMips64 ? &Mips::FGR64RegClass : &Mips::AFGR64RegClass;
02616       else
02617         llvm_unreachable("RegVT not supported by FormalArguments Lowering");
02618 
02619       // Transform the arguments stored on
02620       // physical registers into virtual ones
02621       unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
02622       SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
02623 
02624       // If this is an 8 or 16-bit value, it has been passed promoted
02625       // to 32 bits.  Insert an assert[sz]ext to capture this, then
02626       // truncate to the right size.
02627       if (VA.getLocInfo() != CCValAssign::Full) {
02628         unsigned Opcode = 0;
02629         if (VA.getLocInfo() == CCValAssign::SExt)
02630           Opcode = ISD::AssertSext;
02631         else if (VA.getLocInfo() == CCValAssign::ZExt)
02632           Opcode = ISD::AssertZext;
02633         if (Opcode)
02634           ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
02635                                  DAG.getValueType(ValVT));
02636         ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
02637       }
02638 
02639       // Handle floating point arguments passed in integer registers and
02640       // long double arguments passed in floating point registers.
02641       if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
02642           (RegVT == MVT::i64 && ValVT == MVT::f64) ||
02643           (RegVT == MVT::f64 && ValVT == MVT::i64))
02644         ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
02645       else if (IsO32 && RegVT == MVT::i32 && ValVT == MVT::f64) {
02646         unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
02647                                   getNextIntArgReg(ArgReg), RC);
02648         SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
02649         if (!Subtarget->isLittle())
02650           std::swap(ArgValue, ArgValue2);
02651         ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
02652                                ArgValue, ArgValue2);
02653       }
02654 
02655       InVals.push_back(ArgValue);
02656     } else { // VA.isRegLoc()
02657 
02658       // sanity check
02659       assert(VA.isMemLoc());
02660 
02661       // The stack pointer offset is relative to the caller stack frame.
02662       int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
02663                                       VA.getLocMemOffset(), true);
02664 
02665       // Create load nodes to retrieve arguments from the stack
02666       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
02667       InVals.push_back(DAG.getLoad(ValVT, DL, Chain, FIN,
02668                                    MachinePointerInfo::getFixedStack(FI),
02669                                    false, false, false, 0));
02670     }
02671   }
02672 
02673   // The mips ABIs for returning structs by value requires that we copy
02674   // the sret argument into $v0 for the return. Save the argument into
02675   // a virtual register so that we can access it from the return points.
02676   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
02677     unsigned Reg = MipsFI->getSRetReturnReg();
02678     if (!Reg) {
02679       Reg = MF.getRegInfo().
02680         createVirtualRegister(getRegClassFor(IsN64 ? MVT::i64 : MVT::i32));
02681       MipsFI->setSRetReturnReg(Reg);
02682     }
02683     SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[0]);
02684     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
02685   }
02686 
02687   if (IsVarArg)
02688     writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
02689 
02690   // All stores are grouped in one node to allow the matching between
02691   // the size of Ins and InVals. This only happens when on varg functions
02692   if (!OutChains.empty()) {
02693     OutChains.push_back(Chain);
02694     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
02695                         &OutChains[0], OutChains.size());
02696   }
02697 
02698   return Chain;
02699 }
02700 
02701 //===----------------------------------------------------------------------===//
02702 //               Return Value Calling Convention Implementation
02703 //===----------------------------------------------------------------------===//
02704 
02705 bool
02706 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
02707                                    MachineFunction &MF, bool IsVarArg,
02708                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
02709                                    LLVMContext &Context) const {
02710   SmallVector<CCValAssign, 16> RVLocs;
02711   CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
02712                  RVLocs, Context);
02713   return CCInfo.CheckReturn(Outs, RetCC_Mips);
02714 }
02715 
02716 SDValue
02717 MipsTargetLowering::LowerReturn(SDValue Chain,
02718                                 CallingConv::ID CallConv, bool IsVarArg,
02719                                 const SmallVectorImpl<ISD::OutputArg> &Outs,
02720                                 const SmallVectorImpl<SDValue> &OutVals,
02721                                 DebugLoc DL, SelectionDAG &DAG) const {
02722   // CCValAssign - represent the assignment of
02723   // the return value to a location
02724   SmallVector<CCValAssign, 16> RVLocs;
02725   MachineFunction &MF = DAG.getMachineFunction();
02726 
02727   // CCState - Info about the registers and stack slot.
02728   CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
02729                  *DAG.getContext());
02730   MipsCC MipsCCInfo(CallConv, IsO32, CCInfo);
02731 
02732   // Analyze return values.
02733   MipsCCInfo.analyzeReturn(Outs, getTargetMachine().Options.UseSoftFloat,
02734                            MF.getFunction()->getReturnType());
02735 
02736   SDValue Flag;
02737   SmallVector<SDValue, 4> RetOps(1, Chain);
02738 
02739   // Copy the result values into the output registers.
02740   for (unsigned i = 0; i != RVLocs.size(); ++i) {
02741     SDValue Val = OutVals[i];
02742     CCValAssign &VA = RVLocs[i];
02743     assert(VA.isRegLoc() && "Can only return in registers!");
02744 
02745     if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
02746       Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
02747 
02748     Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
02749 
02750     // Guarantee that all emitted copies are stuck together with flags.
02751     Flag = Chain.getValue(1);
02752     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
02753   }
02754 
02755   // The mips ABIs for returning structs by value requires that we copy
02756   // the sret argument into $v0 for the return. We saved the argument into
02757   // a virtual register in the entry block, so now we copy the value out
02758   // and into $v0.
02759   if (MF.getFunction()->hasStructRetAttr()) {
02760     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
02761     unsigned Reg = MipsFI->getSRetReturnReg();
02762 
02763     if (!Reg)
02764       llvm_unreachable("sret virtual register not created in the entry block");
02765     SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
02766     unsigned V0 = IsN64 ? Mips::V0_64 : Mips::V0;
02767 
02768     Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
02769     Flag = Chain.getValue(1);
02770     RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
02771   }
02772 
02773   RetOps[0] = Chain;  // Update chain.
02774 
02775   // Add the flag if we have it.
02776   if (Flag.getNode())
02777     RetOps.push_back(Flag);
02778 
02779   // Return on Mips is always a "jr $ra"
02780   return DAG.getNode(MipsISD::Ret, DL, MVT::Other, &RetOps[0], RetOps.size());
02781 }
02782 
02783 //===----------------------------------------------------------------------===//
02784 //                           Mips Inline Assembly Support
02785 //===----------------------------------------------------------------------===//
02786 
02787 /// getConstraintType - Given a constraint letter, return the type of
02788 /// constraint it is for this target.
02789 MipsTargetLowering::ConstraintType MipsTargetLowering::
02790 getConstraintType(const std::string &Constraint) const
02791 {
02792   // Mips specific constrainy
02793   // GCC config/mips/constraints.md
02794   //
02795   // 'd' : An address register. Equivalent to r
02796   //       unless generating MIPS16 code.
02797   // 'y' : Equivalent to r; retained for
02798   //       backwards compatibility.
02799   // 'c' : A register suitable for use in an indirect
02800   //       jump. This will always be $25 for -mabicalls.
02801   // 'l' : The lo register. 1 word storage.
02802   // 'x' : The hilo register pair. Double word storage.
02803   if (Constraint.size() == 1) {
02804     switch (Constraint[0]) {
02805       default : break;
02806       case 'd':
02807       case 'y':
02808       case 'f':
02809       case 'c':
02810       case 'l':
02811       case 'x':
02812         return C_RegisterClass;
02813       case 'R':
02814         return C_Memory;
02815     }
02816   }
02817   return TargetLowering::getConstraintType(Constraint);
02818 }
02819 
02820 /// Examine constraint type and operand type and determine a weight value.
02821 /// This object must already have been set up with the operand type
02822 /// and the current alternative constraint selected.
02823 TargetLowering::ConstraintWeight
02824 MipsTargetLowering::getSingleConstraintMatchWeight(
02825     AsmOperandInfo &info, const char *constraint) const {
02826   ConstraintWeight weight = CW_Invalid;
02827   Value *CallOperandVal = info.CallOperandVal;
02828     // If we don't have a value, we can't do a match,
02829     // but allow it at the lowest weight.
02830   if (CallOperandVal == NULL)
02831     return CW_Default;
02832   Type *type = CallOperandVal->getType();
02833   // Look at the constraint type.
02834   switch (*constraint) {
02835   default:
02836     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
02837     break;
02838   case 'd':
02839   case 'y':
02840     if (type->isIntegerTy())
02841       weight = CW_Register;
02842     break;
02843   case 'f':
02844     if (type->isFloatTy())
02845       weight = CW_Register;
02846     break;
02847   case 'c': // $25 for indirect jumps
02848   case 'l': // lo register
02849   case 'x': // hilo register pair
02850       if (type->isIntegerTy())
02851       weight = CW_SpecificReg;
02852       break;
02853   case 'I': // signed 16 bit immediate
02854   case 'J': // integer zero
02855   case 'K': // unsigned 16 bit immediate
02856   case 'L': // signed 32 bit immediate where lower 16 bits are 0
02857   case 'N': // immediate in the range of -65535 to -1 (inclusive)
02858   case 'O': // signed 15 bit immediate (+- 16383)
02859   case 'P': // immediate in the range of 65535 to 1 (inclusive)
02860     if (isa<ConstantInt>(CallOperandVal))
02861       weight = CW_Constant;
02862     break;
02863   case 'R':
02864     weight = CW_Memory;
02865     break;
02866   }
02867   return weight;
02868 }
02869 
02870 /// Given a register class constraint, like 'r', if this corresponds directly
02871 /// to an LLVM register class, return a register of 0 and the register class
02872 /// pointer.
02873 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
02874 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
02875 {
02876   if (Constraint.size() == 1) {
02877     switch (Constraint[0]) {
02878     case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
02879     case 'y': // Same as 'r'. Exists for compatibility.
02880     case 'r':
02881       if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
02882         if (Subtarget->inMips16Mode())
02883           return std::make_pair(0U, &Mips::CPU16RegsRegClass);
02884         return std::make_pair(0U, &Mips::CPURegsRegClass);
02885       }
02886       if (VT == MVT::i64 && !HasMips64)
02887         return std::make_pair(0U, &Mips::CPURegsRegClass);
02888       if (VT == MVT::i64 && HasMips64)
02889         return std::make_pair(0U, &Mips::CPU64RegsRegClass);
02890       // This will generate an error message
02891       return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
02892     case 'f':
02893       if (VT == MVT::f32)
02894         return std::make_pair(0U, &Mips::FGR32RegClass);
02895       if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
02896         if (Subtarget->isFP64bit())
02897           return std::make_pair(0U, &Mips::FGR64RegClass);
02898         return std::make_pair(0U, &Mips::AFGR64RegClass);
02899       }
02900       break;
02901     case 'c': // register suitable for indirect jump
02902       if (VT == MVT::i32)
02903         return std::make_pair((unsigned)Mips::T9, &Mips::CPURegsRegClass);
02904       assert(VT == MVT::i64 && "Unexpected type.");
02905       return std::make_pair((unsigned)Mips::T9_64, &Mips::CPU64RegsRegClass);
02906     case 'l': // register suitable for indirect jump
02907       if (VT == MVT::i32)
02908         return std::make_pair((unsigned)Mips::LO, &Mips::LORegsRegClass);
02909       return std::make_pair((unsigned)Mips::LO64, &Mips::LORegs64RegClass);
02910     case 'x': // register suitable for indirect jump
02911       // Fixme: Not triggering the use of both hi and low
02912       // This will generate an error message
02913       return std::make_pair(0u, static_cast<const TargetRegisterClass*>(0));
02914     }
02915   }
02916   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
02917 }
02918 
02919 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
02920 /// vector.  If it is invalid, don't add anything to Ops.
02921 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
02922                                                      std::string &Constraint,
02923                                                      std::vector<SDValue>&Ops,
02924                                                      SelectionDAG &DAG) const {
02925   SDValue Result(0, 0);
02926 
02927   // Only support length 1 constraints for now.
02928   if (Constraint.length() > 1) return;
02929 
02930   char ConstraintLetter = Constraint[0];
02931   switch (ConstraintLetter) {
02932   default: break; // This will fall through to the generic implementation
02933   case 'I': // Signed 16 bit constant
02934     // If this fails, the parent routine will give an error
02935     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
02936       EVT Type = Op.getValueType();
02937       int64_t Val = C->getSExtValue();
02938       if (isInt<16>(Val)) {
02939         Result = DAG.getTargetConstant(Val, Type);
02940         break;
02941       }
02942     }
02943     return;
02944   case 'J': // integer zero
02945     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
02946       EVT Type = Op.getValueType();
02947       int64_t Val = C->getZExtValue();
02948       if (Val == 0) {
02949         Result = DAG.getTargetConstant(0, Type);
02950         break;
02951       }
02952     }
02953     return;
02954   case 'K': // unsigned 16 bit immediate
02955     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
02956       EVT Type = Op.getValueType();
02957       uint64_t Val = (uint64_t)C->getZExtValue();
02958       if (isUInt<16>(Val)) {
02959         Result = DAG.getTargetConstant(Val, Type);
02960         break;
02961       }
02962     }
02963     return;
02964   case 'L': // signed 32 bit immediate where lower 16 bits are 0
02965     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
02966       EVT Type = Op.getValueType();
02967       int64_t Val = C->getSExtValue();
02968       if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
02969         Result = DAG.getTargetConstant(Val, Type);
02970         break;
02971       }
02972     }
02973     return;
02974   case 'N': // immediate in the range of -65535 to -1 (inclusive)
02975     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
02976       EVT Type = Op.getValueType();
02977       int64_t Val = C->getSExtValue();
02978       if ((Val >= -65535) && (Val <= -1)) {
02979         Result = DAG.getTargetConstant(Val, Type);
02980         break;
02981       }
02982     }
02983     return;
02984   case 'O': // signed 15 bit immediate
02985     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
02986       EVT Type = Op.getValueType();
02987       int64_t Val = C->getSExtValue();
02988       if ((isInt<15>(Val))) {
02989         Result = DAG.getTargetConstant(Val, Type);
02990         break;
02991       }
02992     }
02993     return;
02994   case 'P': // immediate in the range of 1 to 65535 (inclusive)
02995     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
02996       EVT Type = Op.getValueType();
02997       int64_t Val = C->getSExtValue();
02998       if ((Val <= 65535) && (Val >= 1)) {
02999         Result = DAG.getTargetConstant(Val, Type);
03000         break;
03001       }
03002     }
03003     return;
03004   }
03005 
03006   if (Result.getNode()) {
03007     Ops.push_back(Result);
03008     return;
03009   }
03010 
03011   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
03012 }
03013 
03014 bool
03015 MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM, Type *Ty) const {
03016   // No global is ever allowed as a base.
03017   if (AM.BaseGV)
03018     return false;
03019 
03020   switch (AM.Scale) {
03021   case 0: // "r+i" or just "i", depending on HasBaseReg.
03022     break;
03023   case 1:
03024     if (!AM.HasBaseReg) // allow "r+i".
03025       break;
03026     return false; // disallow "r+r" or "r+r+i".
03027   default:
03028     return false;
03029   }
03030 
03031   return true;
03032 }
03033 
03034 bool
03035 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
03036   // The Mips target isn't yet aware of offsets.
03037   return false;
03038 }
03039 
03040 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
03041                                             unsigned SrcAlign,
03042                                             bool IsMemset, bool ZeroMemset,
03043                                             bool MemcpyStrSrc,
03044                                             MachineFunction &MF) const {
03045   if (Subtarget->hasMips64())
03046     return MVT::i64;
03047 
03048   return MVT::i32;
03049 }
03050 
03051 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
03052   if (VT != MVT::f32 && VT != MVT::f64)
03053     return false;
03054   if (Imm.isNegZero())
03055     return false;
03056   return Imm.isZero();
03057 }
03058 
03059 unsigned MipsTargetLowering::getJumpTableEncoding() const {
03060   if (IsN64)
03061     return MachineJumpTableInfo::EK_GPRel64BlockAddress;
03062 
03063   return TargetLowering::getJumpTableEncoding();
03064 }
03065 
03066 /// This function returns true if CallSym is a long double emulation routine.
03067 static bool isF128SoftLibCall(const char *CallSym) {
03068   const char *const LibCalls[] =
03069     {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
03070      "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
03071      "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
03072      "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
03073      "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
03074      "__trunctfdf2", "__trunctfsf2", "__unordtf2",
03075      "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
03076      "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
03077      "truncl"};
03078 
03079   const char * const *End = LibCalls + array_lengthof(LibCalls);
03080 
03081   // Check that LibCalls is sorted alphabetically.
03082   MipsTargetLowering::LTStr Comp;
03083 
03084 #ifndef NDEBUG
03085   for (const char * const *I = LibCalls; I < End - 1; ++I)
03086     assert(Comp(*I, *(I + 1)));
03087 #endif
03088 
03089   return std::binary_search(LibCalls, End, CallSym, Comp);
03090 }
03091 
03092 /// This function returns true if Ty is fp128 or i128 which was originally a
03093 /// fp128.
03094 static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
03095   if (Ty->isFP128Ty())
03096     return true;
03097 
03098   const ExternalSymbolSDNode *ES =
03099     dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
03100 
03101   // If the Ty is i128 and the function being called is a long double emulation
03102   // routine, then the original type is f128.
03103   return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
03104 }
03105 
03106 MipsTargetLowering::MipsCC::SpecialCallingConvType
03107   MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
03108   MipsCC::SpecialCallingConvType SpecialCallingConv =
03109     MipsCC::NoSpecialCallingConv;;
03110   if (Subtarget->inMips16HardFloat()) {
03111     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
03112       llvm::StringRef Sym = G->getGlobal()->getName();
03113       Function *F = G->getGlobal()->getParent()->getFunction(Sym);
03114       if (F->hasFnAttribute("__Mips16RetHelper")) {
03115         SpecialCallingConv = MipsCC::Mips16RetHelperConv;
03116       }
03117     }
03118   }
03119   return SpecialCallingConv;
03120 }
03121 
03122 MipsTargetLowering::MipsCC::MipsCC(
03123   CallingConv::ID CC, bool IsO32_, CCState &Info,
03124     MipsCC::SpecialCallingConvType SpecialCallingConv_)
03125   : CCInfo(Info), CallConv(CC), IsO32(IsO32_),
03126     SpecialCallingConv(SpecialCallingConv_){
03127   // Pre-allocate reserved argument area.
03128   CCInfo.AllocateStack(reservedArgArea(), 1);
03129 }
03130 
03131 
03132 void MipsTargetLowering::MipsCC::
03133 analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
03134                     bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
03135                     std::vector<ArgListEntry> &FuncArgs) {
03136   assert((CallConv != CallingConv::Fast || !IsVarArg) &&
03137          "CallingConv::Fast shouldn't be used for vararg functions.");
03138 
03139   unsigned NumOpnds = Args.size();
03140   llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
03141 
03142   for (unsigned I = 0; I != NumOpnds; ++I) {
03143     MVT ArgVT = Args[I].VT;
03144     ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
03145     bool R;
03146 
03147     if (ArgFlags.isByVal()) {
03148       handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
03149       continue;
03150     }
03151 
03152     if (IsVarArg && !Args[I].IsFixed)
03153       R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
03154     else {
03155       MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
03156                            IsSoftFloat);
03157       R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
03158     }
03159 
03160     if (R) {
03161 #ifndef NDEBUG
03162       dbgs() << "Call operand #" << I << " has unhandled type "
03163              << EVT(ArgVT).getEVTString();
03164 #endif
03165       llvm_unreachable(0);
03166     }
03167   }
03168 }
03169 
03170 void MipsTargetLowering::MipsCC::
03171 analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
03172                        bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
03173   unsigned NumArgs = Args.size();
03174   llvm::CCAssignFn *FixedFn = fixedArgFn();
03175   unsigned CurArgIdx = 0;
03176 
03177   for (unsigned I = 0; I != NumArgs; ++I) {
03178     MVT ArgVT = Args[I].VT;
03179     ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
03180     std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
03181     CurArgIdx = Args[I].OrigArgIndex;
03182 
03183     if (ArgFlags.isByVal()) {
03184       handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
03185       continue;
03186     }
03187 
03188     MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), 0, IsSoftFloat);
03189 
03190     if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
03191       continue;
03192 
03193 #ifndef NDEBUG
03194     dbgs() << "Formal Arg #" << I << " has unhandled type "
03195            << EVT(ArgVT).getEVTString();
03196 #endif
03197     llvm_unreachable(0);
03198   }
03199 }
03200 
03201 template<typename Ty>
03202 void MipsTargetLowering::MipsCC::
03203 analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
03204               const SDNode *CallNode, const Type *RetTy) const {
03205   CCAssignFn *Fn;
03206 
03207   if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
03208     Fn = RetCC_F128Soft;
03209   else
03210     Fn = RetCC_Mips;
03211 
03212   for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
03213     MVT VT = RetVals[I].VT;
03214     ISD::ArgFlagsTy Flags = RetVals[I].Flags;
03215     MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
03216 
03217     if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
03218 #ifndef NDEBUG
03219       dbgs() << "Call result #" << I << " has unhandled type "
03220              << EVT(VT).getEVTString() << '\n';
03221 #endif
03222       llvm_unreachable(0);
03223     }
03224   }
03225 }
03226 
03227 void MipsTargetLowering::MipsCC::
03228 analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
03229                   const SDNode *CallNode, const Type *RetTy) const {
03230   analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
03231 }
03232 
03233 void MipsTargetLowering::MipsCC::
03234 analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
03235               const Type *RetTy) const {
03236   analyzeReturn(Outs, IsSoftFloat, 0, RetTy);
03237 }
03238 
03239 void
03240 MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
03241                                            MVT LocVT,
03242                                            CCValAssign::LocInfo LocInfo,
03243                                            ISD::ArgFlagsTy ArgFlags) {
03244   assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
03245 
03246   struct ByValArgInfo ByVal;
03247   unsigned RegSize = regSize();
03248   unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
03249   unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
03250                             RegSize * 2);
03251 
03252   if (useRegsForByval())
03253     allocateRegs(ByVal, ByValSize, Align);
03254 
03255   // Allocate space on caller's stack.
03256   ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
03257                                        Align);
03258   CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
03259                                     LocInfo));
03260   ByValArgs.push_back(ByVal);
03261 }
03262 
03263 unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
03264   return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
03265 }
03266 
03267 unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
03268   return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
03269 }
03270 
03271 const uint16_t *MipsTargetLowering::MipsCC::intArgRegs() const {
03272   return IsO32 ? O32IntRegs : Mips64IntRegs;
03273 }
03274 
03275 llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
03276   if (CallConv == CallingConv::Fast)
03277     return CC_Mips_FastCC;
03278 
03279   if (SpecialCallingConv == Mips16RetHelperConv)
03280     return CC_Mips16RetHelper;
03281   return IsO32 ? CC_MipsO32 : CC_MipsN;
03282 }
03283 
03284 llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
03285   return IsO32 ? CC_MipsO32 : CC_MipsN_VarArg;
03286 }
03287 
03288 const uint16_t *MipsTargetLowering::MipsCC::shadowRegs() const {
03289   return IsO32 ? O32IntRegs : Mips64DPRegs;
03290 }
03291 
03292 void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
03293                                               unsigned ByValSize,
03294                                               unsigned Align) {
03295   unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
03296   const uint16_t *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
03297   assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
03298          "Byval argument's size and alignment should be a multiple of"
03299          "RegSize.");
03300 
03301   ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
03302 
03303   // If Align > RegSize, the first arg register must be even.
03304   if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
03305     CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
03306     ++ByVal.FirstIdx;
03307   }
03308 
03309   // Mark the registers allocated.
03310   for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
03311        ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
03312     CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
03313 }
03314 
03315 MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
03316                                          const SDNode *CallNode,
03317                                          bool IsSoftFloat) const {
03318   if (IsSoftFloat || IsO32)
03319     return VT;
03320 
03321   // Check if the original type was fp128.
03322   if (originalTypeIsF128(OrigTy, CallNode)) {
03323     assert(VT == MVT::i64);
03324     return MVT::f64;
03325   }
03326 
03327   return VT;
03328 }
03329 
03330 void MipsTargetLowering::
03331 copyByValRegs(SDValue Chain, DebugLoc DL, std::vector<SDValue> &OutChains,
03332               SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
03333               SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
03334               const MipsCC &CC, const ByValArgInfo &ByVal) const {
03335   MachineFunction &MF = DAG.getMachineFunction();
03336   MachineFrameInfo *MFI = MF.getFrameInfo();
03337   unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
03338   unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
03339   int FrameObjOffset;
03340 
03341   if (RegAreaSize)
03342     FrameObjOffset = (int)CC.reservedArgArea() -
03343       (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
03344   else
03345     FrameObjOffset = ByVal.Address;
03346 
03347   // Create frame object.
03348   EVT PtrTy = getPointerTy();
03349   int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
03350   SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
03351   InVals.push_back(FIN);
03352 
03353   if (!ByVal.NumRegs)
03354     return;
03355 
03356   // Copy arg registers.
03357   MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
03358   const TargetRegisterClass *RC = getRegClassFor(RegTy);
03359 
03360   for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
03361     unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
03362     unsigned VReg = addLiveIn(MF, ArgReg, RC);
03363     unsigned Offset = I * CC.regSize();
03364     SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
03365                                    DAG.getConstant(Offset, PtrTy));
03366     SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
03367                                  StorePtr, MachinePointerInfo(FuncArg, Offset),
03368                                  false, false, 0);
03369     OutChains.push_back(Store);
03370   }
03371 }
03372 
03373 // Copy byVal arg to registers and stack.
03374 void MipsTargetLowering::
03375 passByValArg(SDValue Chain, DebugLoc DL,
03376              std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
03377              SmallVector<SDValue, 8> &MemOpChains, SDValue StackPtr,
03378              MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
03379              const MipsCC &CC, const ByValArgInfo &ByVal,
03380              const ISD::ArgFlagsTy &Flags, bool isLittle) const {
03381   unsigned ByValSize = Flags.getByValSize();
03382   unsigned Offset = 0; // Offset in # of bytes from the beginning of struct.
03383   unsigned RegSize = CC.regSize();
03384   unsigned Alignment = std::min(Flags.getByValAlign(), RegSize);
03385   EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSize * 8);
03386 
03387   if (ByVal.NumRegs) {
03388     const uint16_t *ArgRegs = CC.intArgRegs();
03389     bool LeftoverBytes = (ByVal.NumRegs * RegSize > ByValSize);
03390     unsigned I = 0;
03391 
03392     // Copy words to registers.
03393     for (; I < ByVal.NumRegs - LeftoverBytes; ++I, Offset += RegSize) {
03394       SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
03395                                     DAG.getConstant(Offset, PtrTy));
03396       SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
03397                                     MachinePointerInfo(), false, false, false,
03398                                     Alignment);
03399       MemOpChains.push_back(LoadVal.getValue(1));
03400       unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
03401       RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
03402     }
03403 
03404     // Return if the struct has been fully copied.
03405     if (ByValSize == Offset)
03406       return;
03407 
03408     // Copy the remainder of the byval argument with sub-word loads and shifts.
03409     if (LeftoverBytes) {
03410       assert((ByValSize > Offset) && (ByValSize < Offset + RegSize) &&
03411              "Size of the remainder should be smaller than RegSize.");
03412       SDValue Val;
03413 
03414       for (unsigned LoadSize = RegSize / 2, TotalSizeLoaded = 0;
03415            Offset < ByValSize; LoadSize /= 2) {
03416         unsigned RemSize = ByValSize - Offset;
03417 
03418         if (RemSize < LoadSize)
03419           continue;
03420 
03421         // Load subword.
03422         SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
03423                                       DAG.getConstant(Offset, PtrTy));
03424         SDValue LoadVal =
03425           DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr,
03426                          MachinePointerInfo(), MVT::getIntegerVT(LoadSize * 8),
03427                          false, false, Alignment);
03428         MemOpChains.push_back(LoadVal.getValue(1));
03429 
03430         // Shift the loaded value.
03431         unsigned Shamt;
03432 
03433         if (isLittle)
03434           Shamt = TotalSizeLoaded;
03435         else
03436           Shamt = (RegSize - (TotalSizeLoaded + LoadSize)) * 8;
03437 
03438         SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
03439                                     DAG.getConstant(Shamt, MVT::i32));
03440 
03441         if (Val.getNode())
03442           Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
03443         else
03444           Val = Shift;
03445 
03446         Offset += LoadSize;
03447         TotalSizeLoaded += LoadSize;
03448         Alignment = std::min(Alignment, LoadSize);
03449       }
03450 
03451       unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
03452       RegsToPass.push_back(std::make_pair(ArgReg, Val));
03453       return;
03454     }
03455   }
03456 
03457   // Copy remainder of byval arg to it with memcpy.
03458   unsigned MemCpySize = ByValSize - Offset;
03459   SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
03460                             DAG.getConstant(Offset, PtrTy));
03461   SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
03462                             DAG.getIntPtrConstant(ByVal.Address));
03463   Chain = DAG.getMemcpy(Chain, DL, Dst, Src,
03464                         DAG.getConstant(MemCpySize, PtrTy), Alignment,
03465                         /*isVolatile=*/false, /*AlwaysInline=*/false,
03466                         MachinePointerInfo(0), MachinePointerInfo(0));
03467   MemOpChains.push_back(Chain);
03468 }
03469 
03470 void
03471 MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
03472                                     const MipsCC &CC, SDValue Chain,
03473                                     DebugLoc DL, SelectionDAG &DAG) const {
03474   unsigned NumRegs = CC.numIntArgRegs();
03475   const uint16_t *ArgRegs = CC.intArgRegs();
03476   const CCState &CCInfo = CC.getCCInfo();
03477   unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
03478   unsigned RegSize = CC.regSize();
03479   MVT RegTy = MVT::getIntegerVT(RegSize * 8);
03480   const TargetRegisterClass *RC = getRegClassFor(RegTy);
03481   MachineFunction &MF = DAG.getMachineFunction();
03482   MachineFrameInfo *MFI = MF.getFrameInfo();
03483   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
03484 
03485   // Offset of the first variable argument from stack pointer.
03486   int VaArgOffset;
03487 
03488   if (NumRegs == Idx)
03489     VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
03490   else
03491     VaArgOffset =
03492       (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
03493 
03494   // Record the frame index of the first variable argument
03495   // which is a value necessary to VASTART.
03496   int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
03497   MipsFI->setVarArgsFrameIndex(FI);
03498 
03499   // Copy the integer registers that have not been used for argument passing
03500   // to the argument register save area. For O32, the save area is allocated
03501   // in the caller's stack frame, while for N32/64, it is allocated in the
03502   // callee's stack frame.
03503   for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
03504     unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
03505     SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
03506     FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
03507     SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
03508     SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
03509                                  MachinePointerInfo(), false, false, 0);
03510     cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue(0);
03511     OutChains.push_back(Store);
03512   }
03513 }