Skip to content
X86ISelLowering.cpp 119 KiB
Newer Older
      Ops.push_back(Tmp3);
      Ops.push_back(Tmp1);
      Ops.push_back(CC);
      Ops.push_back(InFlag);
      Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
    } else {
      Ops.push_back(Tmp2);
      Ops.push_back(Tmp3);
      Ops.push_back(CC);
Evan Cheng's avatar
Evan Cheng committed
      Ops.push_back(InFlag);
      Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
      InFlag = Lo.getValue(1);

      Ops.clear();
      Ops.push_back(Tmp3);
      Ops.push_back(Tmp1);
      Ops.push_back(CC);
      Ops.push_back(InFlag);
      Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
    }

    Tys.clear();
    Tys.push_back(MVT::i32);
    Tys.push_back(MVT::i32);
    Ops.clear();
    Ops.push_back(Lo);
    Ops.push_back(Hi);
    return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
  }
    assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
           Op.getOperand(0).getValueType() >= MVT::i16 &&

    SDOperand Result;
    MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
    unsigned Size = MVT::getSizeInBits(SrcVT)/8;
    MachineFunction &MF = DAG.getMachineFunction();
    int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
    SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
    SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
                                  DAG.getEntryNode(), Op.getOperand(0),
                                  StackSlot, DAG.getSrcValue(NULL));

    // Build the FILD
    std::vector<MVT::ValueType> Tys;
    Tys.push_back(MVT::f64);
    if (X86ScalarSSE) Tys.push_back(MVT::Flag);
    Ops.push_back(Chain);
    Ops.push_back(DAG.getValueType(SrcVT));
    Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
                         Tys, Ops);

    if (X86ScalarSSE) {
      Chain = Result.getValue(1);
      SDOperand InFlag = Result.getValue(2);

      // FIXME: Currently the FST is flagged to the FILD_FLAG. This
      // shouldn't be necessary except that RFP cannot be live across
      // multiple blocks. When stackifier is fixed, they can be uncoupled.
      MachineFunction &MF = DAG.getMachineFunction();
      int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
      SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
      std::vector<MVT::ValueType> Tys;
      Tys.push_back(MVT::Other);
      std::vector<SDOperand> Ops;
      Ops.push_back(Chain);
      Ops.push_back(Result);
      Ops.push_back(StackSlot);
      Ops.push_back(DAG.getValueType(Op.getValueType()));
      Ops.push_back(InFlag);
      Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
      Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
                           DAG.getSrcValue(NULL));
    }

    return Result;
  }
  case ISD::FP_TO_SINT: {
    assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
           "Unknown FP_TO_SINT to lower!");
    // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
    // stack slot.
    MachineFunction &MF = DAG.getMachineFunction();
    unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
    int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
    SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());

    unsigned Opc;
    switch (Op.getValueType()) {
    default: assert(0 && "Invalid FP_TO_SINT to lower!");
    case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
    case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
    case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
    }

    SDOperand Chain = DAG.getEntryNode();
    SDOperand Value = Op.getOperand(0);
    if (X86ScalarSSE) {
      assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
      Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot, 
                          DAG.getSrcValue(0));
      std::vector<MVT::ValueType> Tys;
      Tys.push_back(MVT::f64);
      Tys.push_back(MVT::Other);
      std::vector<SDOperand> Ops;
      Ops.push_back(Chain);
      Ops.push_back(StackSlot);
      Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
      Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
      Chain = Value.getValue(1);
      SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
      StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
    }

    // Build the FP_TO_INT*_IN_MEM
    std::vector<SDOperand> Ops;
    Ops.push_back(Chain);
    Ops.push_back(Value);
    Ops.push_back(StackSlot);
    SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);

    // Load the result.
    return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
                       DAG.getSrcValue(NULL));
  }
  case ISD::READCYCLECOUNTER: {
    std::vector<MVT::ValueType> Tys;
    Tys.push_back(MVT::Other);
    Tys.push_back(MVT::Flag);
    std::vector<SDOperand> Ops;
    Ops.push_back(Op.getOperand(0));
    SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
    Ops.clear();
    Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
    Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, 
                                     MVT::i32, Ops[0].getValue(2)));
    Ops.push_back(Ops[1].getValue(1));
    Tys[0] = Tys[1] = MVT::i32;
    Tys.push_back(MVT::Other);
    return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
  case ISD::FABS: {
    MVT::ValueType VT = Op.getValueType();
    const Type *OpNTy =  MVT::getTypeForValueType(VT);
    std::vector<Constant*> CV;
    if (VT == MVT::f64) {
      CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
    } else {
      CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
    }
    Constant *CS = ConstantStruct::get(CV);
    SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
    SDOperand Mask 
      = DAG.getNode(X86ISD::LOAD_PACK,
                    VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
    return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
  }
  case ISD::FNEG: {
    MVT::ValueType VT = Op.getValueType();
    const Type *OpNTy =  MVT::getTypeForValueType(VT);
    std::vector<Constant*> CV;
    if (VT == MVT::f64) {
      CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
    } else {
      CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
      CV.push_back(ConstantFP::get(OpNTy, 0.0));
    }
    Constant *CS = ConstantStruct::get(CV);
    SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
    SDOperand Mask 
      = DAG.getNode(X86ISD::LOAD_PACK,
                    VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
    return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
  }
  case ISD::SETCC: {
    assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
    SDOperand Cond;
    SDOperand CC = Op.getOperand(2);
Evan Cheng's avatar
Evan Cheng committed
    ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
    bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
    bool Flip;
    unsigned X86CC;
    if (translateX86CC(CC, isFP, X86CC, Flip)) {
      if (Flip)
        Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
                           Op.getOperand(1), Op.getOperand(0));
      else
        Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
                           Op.getOperand(0), Op.getOperand(1));
Evan Cheng's avatar
Evan Cheng committed
      return DAG.getNode(X86ISD::SETCC, MVT::i8, 
                         DAG.getConstant(X86CC, MVT::i8), Cond);
    } else {
      assert(isFP && "Illegal integer SetCC!");

      Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
                         Op.getOperand(0), Op.getOperand(1));
Evan Cheng's avatar
Evan Cheng committed
      std::vector<MVT::ValueType> Tys;
      std::vector<SDOperand> Ops;
      switch (SetCCOpcode) {
      default: assert(false && "Illegal floating point SetCC!");
      case ISD::SETOEQ: {  // !PF & ZF
        Tys.push_back(MVT::i8);
        Tys.push_back(MVT::Flag);
        Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
        Ops.push_back(Cond);
        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
                                     DAG.getConstant(X86ISD::COND_E, MVT::i8),
                                     Tmp1.getValue(1));
        return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
      }
      case ISD::SETUNE: {  // PF | !ZF
        Tys.push_back(MVT::i8);
        Tys.push_back(MVT::Flag);
        Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
        Ops.push_back(Cond);
        SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
        SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
                                     DAG.getConstant(X86ISD::COND_NE, MVT::i8),
                                     Tmp1.getValue(1));
        return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
      }
      }
    }
    MVT::ValueType VT = Op.getValueType();
    bool isFP      = MVT::isFloatingPoint(VT);
    bool isFPStack = isFP && !X86ScalarSSE;
    bool isFPSSE   = isFP && X86ScalarSSE;
    bool addTest   = false;
    SDOperand Op0 = Op.getOperand(0);
    SDOperand Cond, CC;
    if (Op0.getOpcode() == ISD::SETCC)
      Op0 = LowerOperation(Op0, DAG);

    if (Op0.getOpcode() == X86ISD::SETCC) {
      // If condition flag is set by a X86ISD::CMP, then make a copy of it
      // (since flag operand cannot be shared). If the X86ISD::SETCC does not
      // have another use it will be eliminated.
      // If the X86ISD::SETCC has more than one use, then it's probably better
      // to use a test instead of duplicating the X86ISD::CMP (for register
      // pressure reason).
      unsigned CmpOpc = Op0.getOperand(1).getOpcode();
      if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
          CmpOpc == X86ISD::UCOMI) {
        if (!Op0.hasOneUse()) {
          std::vector<MVT::ValueType> Tys;
          for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
            Tys.push_back(Op0.Val->getValueType(i));
          std::vector<SDOperand> Ops;
          for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
            Ops.push_back(Op0.getOperand(i));
          Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
        }

        CC   = Op0.getOperand(0);
        Cond = Op0.getOperand(1);
        // Make a copy as flag result cannot be used by more than one.
        Cond = DAG.getNode(CmpOpc, MVT::Flag,
                           Cond.getOperand(0), Cond.getOperand(1));
        addTest =
          isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
      } else
        addTest = true;
    } else
      addTest = true;
Evan Cheng's avatar
Evan Cheng committed
    if (addTest) {
      CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
      Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);

    std::vector<MVT::ValueType> Tys;
    Tys.push_back(Op.getValueType());
    Tys.push_back(MVT::Flag);
    std::vector<SDOperand> Ops;
    // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
    // condition is true.
    Ops.push_back(Op.getOperand(2));
    Ops.push_back(CC);
    Ops.push_back(Cond);
    return DAG.getNode(X86ISD::CMOV, Tys, Ops);
  case ISD::BRCOND: {
    bool addTest = false;
    SDOperand Cond  = Op.getOperand(1);
    SDOperand Dest  = Op.getOperand(2);
    SDOperand CC;
    if (Cond.getOpcode() == ISD::SETCC)
      Cond = LowerOperation(Cond, DAG);

    if (Cond.getOpcode() == X86ISD::SETCC) {
      // If condition flag is set by a X86ISD::CMP, then make a copy of it
      // (since flag operand cannot be shared). If the X86ISD::SETCC does not
      // have another use it will be eliminated.
      // If the X86ISD::SETCC has more than one use, then it's probably better
      // to use a test instead of duplicating the X86ISD::CMP (for register
      // pressure reason).
      unsigned CmpOpc = Cond.getOperand(1).getOpcode();
      if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
          CmpOpc == X86ISD::UCOMI) {
        if (!Cond.hasOneUse()) {
          std::vector<MVT::ValueType> Tys;
          for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
            Tys.push_back(Cond.Val->getValueType(i));
          std::vector<SDOperand> Ops;
          for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
            Ops.push_back(Cond.getOperand(i));
          Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
        }

        CC   = Cond.getOperand(0);
        Cond = Cond.getOperand(1);
        // Make a copy as flag result cannot be used by more than one.
        Cond = DAG.getNode(CmpOpc, MVT::Flag,
                           Cond.getOperand(0), Cond.getOperand(1));
      } else
        addTest = true;
    } else
      addTest = true;

    if (addTest) {
Evan Cheng's avatar
Evan Cheng committed
      CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
      Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
    }
    return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
                       Op.getOperand(0), Op.getOperand(2), CC, Cond);
  }
  case ISD::MEMSET: {
    SDOperand Chain = Op.getOperand(0);
    unsigned Align =
      (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
    if (Align == 0) Align = 1;

    ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
    // If not DWORD aligned, call memset if size is less than the threshold.
    // It knows how to align to the right boundary first.
        (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
      MVT::ValueType IntPtr = getPointerTy();
      const Type *IntPtrTy = getTargetData().getIntPtrType();
      std::vector<std::pair<SDOperand, const Type*> > Args;
      Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
      // Extend the ubyte argument to be an int value for the call.
      SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
      Args.push_back(std::make_pair(Val, IntPtrTy));
      Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
      std::pair<SDOperand,SDOperand> CallResult =
        LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
                    DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
      return CallResult.second;
    }

    MVT::ValueType AVT;
    SDOperand Count;
    ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
    unsigned BytesLeft = 0;
      unsigned ValReg;
      unsigned Val = ValC->getValue() & 255;

      // If the value is a constant, then we can potentially use larger sets.
      switch (Align & 3) {
      case 2:   // WORD aligned
        AVT = MVT::i16;
        Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
        BytesLeft = I->getValue() % 2;
        Val    = (Val << 8) | Val;
        ValReg = X86::AX;
        break;
      case 0:   // DWORD aligned
        AVT = MVT::i32;
        if (I) {
          Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
          BytesLeft = I->getValue() % 4;
        } else {
          Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
                              DAG.getConstant(2, MVT::i8));
          TwoRepStos = true;
        }
        Val = (Val << 8)  | Val;
        Val = (Val << 16) | Val;
        ValReg = X86::EAX;
        break;
      default:  // Byte aligned
        AVT = MVT::i8;
        Count = Op.getOperand(3);
        ValReg = X86::AL;
        break;
      }

      Chain  = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
                                InFlag);
      InFlag = Chain.getValue(1);
    } else {
      AVT = MVT::i8;
      Count  = Op.getOperand(3);
      Chain  = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
      InFlag = Chain.getValue(1);
    }

    Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
    InFlag = Chain.getValue(1);
    Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
    InFlag = Chain.getValue(1);
    std::vector<MVT::ValueType> Tys;
    Tys.push_back(MVT::Other);
    Tys.push_back(MVT::Flag);
    std::vector<SDOperand> Ops;
    Ops.push_back(Chain);
    Ops.push_back(DAG.getValueType(AVT));
    Ops.push_back(InFlag);
    Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);

    if (TwoRepStos) {
      InFlag = Chain.getValue(1);
      Count = Op.getOperand(3);
      MVT::ValueType CVT = Count.getValueType();
      SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
                                   DAG.getConstant(3, CVT));
      Chain  = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
      InFlag = Chain.getValue(1);
      Tys.clear();
      Tys.push_back(MVT::Other);
      Tys.push_back(MVT::Flag);
      Ops.clear();
      Ops.push_back(Chain);
      Ops.push_back(DAG.getValueType(MVT::i8));
      Ops.push_back(InFlag);
      Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
    } else if (BytesLeft) {
      // Issue stores for the last 1 - 3 bytes.
      SDOperand Value;
      unsigned Val = ValC->getValue() & 255;
      unsigned Offset = I->getValue() - BytesLeft;
      SDOperand DstAddr = Op.getOperand(1);
      MVT::ValueType AddrVT = DstAddr.getValueType();
      if (BytesLeft >= 2) {
        Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
        Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
                            DAG.getNode(ISD::ADD, AddrVT, DstAddr,
                                        DAG.getConstant(Offset, AddrVT)),
                            DAG.getSrcValue(NULL));
        BytesLeft -= 2;
        Offset += 2;
      }

      if (BytesLeft == 1) {
        Value = DAG.getConstant(Val, MVT::i8);
        Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
                            DAG.getNode(ISD::ADD, AddrVT, DstAddr,
                                        DAG.getConstant(Offset, AddrVT)),
                            DAG.getSrcValue(NULL));
      }
    }
  }
  case ISD::MEMCPY: {
    SDOperand Chain = Op.getOperand(0);
    unsigned Align =
      (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
    if (Align == 0) Align = 1;

    ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
    // If not DWORD aligned, call memcpy if size is less than the threshold.
    // It knows how to align to the right boundary first.
        (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
      MVT::ValueType IntPtr = getPointerTy();
      const Type *IntPtrTy = getTargetData().getIntPtrType();
      std::vector<std::pair<SDOperand, const Type*> > Args;
      Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
      Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
      Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
      std::pair<SDOperand,SDOperand> CallResult =
        LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
                    DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
      return CallResult.second;
    }

    MVT::ValueType AVT;
    SDOperand Count;
    switch (Align & 3) {
    case 2:   // WORD aligned
      AVT = MVT::i16;
      Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
      BytesLeft = I->getValue() % 2;
      break;
    case 0:   // DWORD aligned
      AVT = MVT::i32;
      if (I) {
        Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
        BytesLeft = I->getValue() % 4;
      } else {
        Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
                            DAG.getConstant(2, MVT::i8));
        TwoRepMovs = true;
      }
      break;
    default:  // Byte aligned
      AVT = MVT::i8;
      Count = Op.getOperand(3);
      break;
    }

    Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
    InFlag = Chain.getValue(1);
    Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
    InFlag = Chain.getValue(1);
    Chain  = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
    InFlag = Chain.getValue(1);
    std::vector<MVT::ValueType> Tys;
    Tys.push_back(MVT::Other);
    Tys.push_back(MVT::Flag);
    std::vector<SDOperand> Ops;
    Ops.push_back(Chain);
    Ops.push_back(DAG.getValueType(AVT));
    Ops.push_back(InFlag);
    Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);

    if (TwoRepMovs) {
      InFlag = Chain.getValue(1);
      Count = Op.getOperand(3);
      MVT::ValueType CVT = Count.getValueType();
      SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
                                   DAG.getConstant(3, CVT));
      Chain  = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
      InFlag = Chain.getValue(1);
      Tys.clear();
      Tys.push_back(MVT::Other);
      Tys.push_back(MVT::Flag);
      Ops.clear();
      Ops.push_back(Chain);
      Ops.push_back(DAG.getValueType(MVT::i8));
      Ops.push_back(InFlag);
      Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
    } else if (BytesLeft) {
      // Issue loads and stores for the last 1 - 3 bytes.
      unsigned Offset = I->getValue() - BytesLeft;
      SDOperand DstAddr = Op.getOperand(1);
      MVT::ValueType DstVT = DstAddr.getValueType();
      SDOperand SrcAddr = Op.getOperand(2);
      MVT::ValueType SrcVT = SrcAddr.getValueType();
      SDOperand Value;
      if (BytesLeft >= 2) {
        Value = DAG.getLoad(MVT::i16, Chain,
                            DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
                                        DAG.getConstant(Offset, SrcVT)),
                            DAG.getSrcValue(NULL));
        Chain = Value.getValue(1);
        Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
                            DAG.getNode(ISD::ADD, DstVT, DstAddr,
                                        DAG.getConstant(Offset, DstVT)),
                            DAG.getSrcValue(NULL));
        BytesLeft -= 2;
        Offset += 2;
      }

      if (BytesLeft == 1) {
        Value = DAG.getLoad(MVT::i8, Chain,
                            DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
                                        DAG.getConstant(Offset, SrcVT)),
                            DAG.getSrcValue(NULL));
        Chain = Value.getValue(1);
        Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
                            DAG.getNode(ISD::ADD, DstVT, DstAddr,
                                        DAG.getConstant(Offset, DstVT)),
                            DAG.getSrcValue(NULL));
      }
    }

  // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
  // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
  // one of the above mentioned nodes. It has to be wrapped because otherwise
  // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
  // be used to form addressing mode. These wrapped nodes will be selected
  // into MOV32ri.
Evan Cheng's avatar
Evan Cheng committed
  case ISD::ConstantPool: {
    ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
    SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
                         DAG.getTargetConstantPool(CP->get(), getPointerTy(),
                                                   CP->getAlignment()));
Evan Cheng's avatar
Evan Cheng committed
    if (Subtarget->isTargetDarwin()) {
Evan Cheng's avatar
Evan Cheng committed
      // With PIC, the address is actually $g + Offset.
      if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng's avatar
Evan Cheng committed
        Result = DAG.getNode(ISD::ADD, getPointerTy(),
                DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);    
    }

    return Result;
  }
  case ISD::GlobalAddress: {
    GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
    SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
                         DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Cheng's avatar
Evan Cheng committed
    if (Subtarget->isTargetDarwin()) {
Evan Cheng's avatar
Evan Cheng committed
      // With PIC, the address is actually $g + Offset.
      if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng's avatar
Evan Cheng committed
        Result = DAG.getNode(ISD::ADD, getPointerTy(),
                    DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng's avatar
Evan Cheng committed

      // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng's avatar
Evan Cheng committed
      // the value at address GV, not the value of GV itself. This means that
Evan Cheng's avatar
Evan Cheng committed
      // the GlobalAddress must be in the base or index register of the address,
      // not the GV offset field.
      if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng's avatar
Evan Cheng committed
          DarwinGVRequiresExtraLoad(GV))
        Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng's avatar
Evan Cheng committed
                             Result, DAG.getSrcValue(NULL));
Nate Begeman's avatar
Nate Begeman committed
  }
  case ISD::ExternalSymbol: {
    const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
    SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
                         DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Cheng's avatar
Evan Cheng committed
    if (Subtarget->isTargetDarwin()) {
      // With PIC, the address is actually $g + Offset.
      if (getTargetMachine().getRelocationModel() == Reloc::PIC)
        Result = DAG.getNode(ISD::ADD, getPointerTy(),
                    DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
    }

    return Result;
  }
Nate Begeman's avatar
Nate Begeman committed
  case ISD::VASTART: {
    // vastart just stores the address of the VarArgsFrameIndex slot into the
    // memory location argument.
    // FIXME: Replace MVT::i32 with PointerTy
    SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
    return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR, 
                       Op.getOperand(1), Op.getOperand(2));
  }
  case ISD::RET: {
    SDOperand Copy;
    
    switch(Op.getNumOperands()) {
    default:
      assert(0 && "Do not know how to return this many arguments!");
      abort();
    case 1: 
      return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
                         DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
    case 2: {
      MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
      if (MVT::isInteger(ArgVT))
        Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
                                SDOperand());
      else if (!X86ScalarSSE) {
        std::vector<MVT::ValueType> Tys;
        Tys.push_back(MVT::Other);
        Tys.push_back(MVT::Flag);
        std::vector<SDOperand> Ops;
        Ops.push_back(Op.getOperand(0));
        Ops.push_back(Op.getOperand(1));
        Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
      } else {
        SDOperand MemLoc;
        SDOperand Chain = Op.getOperand(0);
        if (Value.getOpcode() == ISD::LOAD &&
            (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
          Chain  = Value.getOperand(0);
          MemLoc = Value.getOperand(1);
        } else {
          // Spill the value to memory and reload it into top of stack.
          unsigned Size = MVT::getSizeInBits(ArgVT)/8;
          MachineFunction &MF = DAG.getMachineFunction();
          int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
          MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
          Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), 
                              Value, MemLoc, DAG.getSrcValue(0));
        }
        std::vector<MVT::ValueType> Tys;
        Tys.push_back(MVT::f64);
        Tys.push_back(MVT::Other);
        std::vector<SDOperand> Ops;
        Ops.push_back(Chain);
        Ops.push_back(DAG.getValueType(ArgVT));
        Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
        Tys.clear();
        Tys.push_back(MVT::Other);
        Tys.push_back(MVT::Flag);
        Ops.clear();
        Ops.push_back(Copy.getValue(1));
        Ops.push_back(Copy);
        Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
      }
      break;
    }
    case 3:
      Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2), 
                              SDOperand());
      Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
      break;
    }
    return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
                       Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
                       Copy.getValue(1));
  case ISD::SCALAR_TO_VECTOR: {
    SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
    return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
  case ISD::VECTOR_SHUFFLE: {
    SDOperand V1 = Op.getOperand(0);
    SDOperand V2 = Op.getOperand(1);
    SDOperand PermMask = Op.getOperand(2);
    MVT::ValueType VT = Op.getValueType();
    unsigned NumElems = PermMask.getNumOperands();
    if (X86::isSplatMask(PermMask.Val))

    // Normalize the node to match x86 shuffle ops if needed
    if (V2.getOpcode() != ISD::UNDEF) {
      bool DoSwap = false;

      if (ShouldXformedToMOVLP(V1, V2, PermMask))
        DoSwap = true;
      else if (isLowerFromV2UpperFromV1(PermMask))
        DoSwap = true;

      if (DoSwap) {
        Op = CommuteVectorShuffle(Op, DAG);
        V1 = Op.getOperand(0);
        V2 = Op.getOperand(1);
        PermMask = Op.getOperand(2);
      }
    if (NumElems == 2)
      return Op;

    if (X86::isUNPCKLMask(PermMask.Val) ||
        X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
        X86::isUNPCKHMask(PermMask.Val))
      // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
    // If VT is integer, try PSHUF* first, then SHUFP*.
    if (MVT::isInteger(VT)) {
      if (X86::isPSHUFDMask(PermMask.Val) ||
          X86::isPSHUFHWMask(PermMask.Val) ||
          X86::isPSHUFLWMask(PermMask.Val)) {
        if (V2.getOpcode() != ISD::UNDEF)
          return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
                             DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
      }

      if (X86::isSHUFPMask(PermMask.Val))
        return Op;

      // Handle v8i16 shuffle high / low shuffle node pair.
      if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
        MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
        MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
        std::vector<SDOperand> MaskVec;
        for (unsigned i = 0; i != 4; ++i)
          MaskVec.push_back(PermMask.getOperand(i));
        for (unsigned i = 4; i != 8; ++i)
          MaskVec.push_back(DAG.getConstant(i, BaseVT));
        SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
        V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
        MaskVec.clear();
        for (unsigned i = 0; i != 4; ++i)
          MaskVec.push_back(DAG.getConstant(i, BaseVT));
        for (unsigned i = 4; i != 8; ++i)
          MaskVec.push_back(PermMask.getOperand(i));
        Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
        return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
      }
    } else {
      // Floating point cases in the other order.
      if (X86::isSHUFPMask(PermMask.Val))
        return Op;
      if (X86::isPSHUFDMask(PermMask.Val) ||
          X86::isPSHUFHWMask(PermMask.Val) ||
          X86::isPSHUFLWMask(PermMask.Val)) {
        if (V2.getOpcode() != ISD::UNDEF)
          return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
                             DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
  case ISD::BUILD_VECTOR: {
    // All one's are handled with pcmpeqd.
    if (ISD::isBuildVectorAllOnes(Op.Val))
      return Op;

    SDOperand Elt0 = Op.getOperand(0);
    bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
                       cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
      (isa<ConstantFPSDNode>(Elt0) &&
       cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
    bool RestAreZero = true;
    unsigned NumElems = Op.getNumOperands();
    for (unsigned i = 1; i < NumElems; ++i) {
      SDOperand Elt = Op.getOperand(i);
      if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
        if (!FPC->isExactlyValue(+0.0))
          RestAreZero = false;
      } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
        if (!C->isNullValue())
          RestAreZero = false;
        RestAreZero = false;
    }

    if (RestAreZero) {
      if (Elt0IsZero) return Op;

      // Zero extend a scalar to a vector.
      return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
    if (Values.size() > 2) {
      // Expand into a number of unpckl*.
      // e.g. for v4f32
      //   Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
      //         : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
      //   Step 2: unpcklps X, Y ==>    <3, 2, 1, 0>
      MVT::ValueType VT = Op.getValueType();
      MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
      MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
      std::vector<SDOperand> MaskVec;
      for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
        MaskVec.push_back(DAG.getConstant(i,            BaseVT));
        MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
      }
      SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
      std::vector<SDOperand> V(NumElems);
      for (unsigned i = 0; i < NumElems; ++i)
        V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
      NumElems >>= 1;
      while (NumElems != 0) {
        for (unsigned i = 0; i < NumElems; ++i)
          V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
                             PermMask);
    return SDOperand();
  }
    if (!isa<ConstantSDNode>(Op.getOperand(1)))
        return SDOperand();

    MVT::ValueType VT = Op.getValueType();
    if (MVT::getSizeInBits(VT) == 16) {
      // Transform it so it match pextrw which produces a 32-bit result.
      MVT::ValueType EVT = (MVT::ValueType)(VT+1);
      SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
                                      Op.getOperand(0), Op.getOperand(1));
      SDOperand Assert  = DAG.getNode(ISD::AssertZext, EVT, Extract,
                                      DAG.getValueType(VT));
      return DAG.getNode(ISD::TRUNCATE, VT, Assert);
    } else if (MVT::getSizeInBits(VT) == 32) {
      SDOperand Vec = Op.getOperand(0);
      unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
      if (Idx == 0)
        return Op;

      // TODO: if Idex == 2, we can use unpckhps
      // SHUFPS the element to the lowest double word, then movss.
      MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
      SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
                                          MVT::getVectorBaseType(MaskVT));
      std::vector<SDOperand> IdxVec;
      IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
      IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
      IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
      IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
      SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
      Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
                        Vec, Vec, Mask);
      return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
                         DAG.getConstant(0, MVT::i32));
    } else if (MVT::getSizeInBits(VT) == 64) {
      SDOperand Vec = Op.getOperand(0);
      unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
      if (Idx == 0)
        return Op;

      // UNPCKHPD the element to the lowest double word, then movsd.
      // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
      // to a f64mem, the whole operation is folded into a single MOVHPDmr.
      MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
      std::vector<SDOperand> IdxVec;
      IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
      IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
      SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
      Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
                        Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
      return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
                         DAG.getConstant(0, MVT::i32));
    }

    return SDOperand();
  }
  case ISD::INSERT_VECTOR_ELT: {
    // Transform it so it match pinsrw which expects a 16-bit value in a R32
    // as its second argument.
    MVT::ValueType VT = Op.getValueType();
    MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
    if (MVT::getSizeInBits(BaseVT) == 16) {
      SDOperand N1 = Op.getOperand(1);
      SDOperand N2 = Op.getOperand(2);
      if (N1.getValueType() != MVT::i32)
        N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
      if (N2.getValueType() != MVT::i32)
        N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
      return DAG.getNode(X86ISD::PINSRW, VT, Op.getOperand(0), N1, N2);
  case ISD::INTRINSIC_WO_CHAIN: {
    unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
    switch (IntNo) {
    default: return SDOperand();    // Don't custom lower most intrinsics.
    // Comparison intrinsics.
    case Intrinsic::x86_sse_comieq_ss:
    case Intrinsic::x86_sse_comilt_ss:
    case Intrinsic::x86_sse_comile_ss:
    case Intrinsic::x86_sse_comigt_ss:
    case Intrinsic::x86_sse_comige_ss:
    case Intrinsic::x86_sse_comineq_ss:
    case Intrinsic::x86_sse_ucomieq_ss:
    case Intrinsic::x86_sse_ucomilt_ss:
    case Intrinsic::x86_sse_ucomile_ss:
    case Intrinsic::x86_sse_ucomigt_ss:
    case Intrinsic::x86_sse_ucomige_ss:
    case Intrinsic::x86_sse_ucomineq_ss:
    case Intrinsic::x86_sse2_comieq_sd:
    case Intrinsic::x86_sse2_comilt_sd:
    case Intrinsic::x86_sse2_comile_sd:
    case Intrinsic::x86_sse2_comigt_sd:
    case Intrinsic::x86_sse2_comige_sd:
    case Intrinsic::x86_sse2_comineq_sd:
    case Intrinsic::x86_sse2_ucomieq_sd:
    case Intrinsic::x86_sse2_ucomilt_sd:
    case Intrinsic::x86_sse2_ucomile_sd:
    case Intrinsic::x86_sse2_ucomigt_sd:
    case Intrinsic::x86_sse2_ucomige_sd:
    case Intrinsic::x86_sse2_ucomineq_sd: {
      unsigned Opc = 0;
      ISD::CondCode CC = ISD::SETCC_INVALID;
      switch (IntNo) {
        default: break;
        case Intrinsic::x86_sse_comieq_ss: 
        case Intrinsic::x86_sse2_comieq_sd: 
          Opc = X86ISD::COMI;
          CC = ISD::SETEQ;
          break;
        case Intrinsic::x86_sse_comilt_ss:
        case Intrinsic::x86_sse2_comilt_sd:
          Opc = X86ISD::COMI;
          CC = ISD::SETLT;
          break;
        case Intrinsic::x86_sse_comile_ss:
        case Intrinsic::x86_sse2_comile_sd:
          Opc = X86ISD::COMI;
          CC = ISD::SETLE;