153c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//===-- X86SelectionDAGInfo.cpp - X86 SelectionDAG Info -------------------===//
253c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//
353c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//                     The LLVM Compiler Infrastructure
453c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//
553c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman// This file is distributed under the University of Illinois Open Source
653c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman// License. See LICENSE.TXT for details.
753c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//
853c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//===----------------------------------------------------------------------===//
953c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//
1053c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman// This file implements the X86SelectionDAGInfo class.
1153c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//
1253c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman//===----------------------------------------------------------------------===//
1353c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman
1453c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman#define DEBUG_TYPE "x86-selectiondag-info"
15ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman#include "X86TargetMachine.h"
16ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman#include "llvm/CodeGen/SelectionDAG.h"
170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
1853c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohmanusing namespace llvm;
1953c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman
20ff7a562751604a9fe13efc75bd59622244b54d35Dan GohmanX86SelectionDAGInfo::X86SelectionDAGInfo(const X86TargetMachine &TM) :
21ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  TargetSelectionDAGInfo(TM),
22ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Subtarget(&TM.getSubtarget<X86Subtarget>()),
23ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  TLI(*TM.getTargetLowering()) {
2453c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman}
2553c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman
2653c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan GohmanX86SelectionDAGInfo::~X86SelectionDAGInfo() {
2753c5e42ab9c1a2cce7ad19bb0b4dffe33c9473e6Dan Gohman}
28ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
29ff7a562751604a9fe13efc75bd59622244b54d35Dan GohmanSDValue
30ff7a562751604a9fe13efc75bd59622244b54d35Dan GohmanX86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
31ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                             SDValue Chain,
32ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                             SDValue Dst, SDValue Src,
33ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                             SDValue Size, unsigned Align,
34ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                             bool isVolatile,
35e72f2027e9116c55a5b39ac72732df8d6c45d37cChris Lattner                                         MachinePointerInfo DstPtrInfo) const {
36ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
37ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
38e54b482d1c6ec714a73d8a87a4f7583aa86fac9fChris Lattner  // If to a segment-relative address space, use the default lowering.
39e54b482d1c6ec714a73d8a87a4f7583aa86fac9fChris Lattner  if (DstPtrInfo.getAddrSpace() >= 256)
40e54b482d1c6ec714a73d8a87a4f7583aa86fac9fChris Lattner    return SDValue();
41a20e1e7ef596842127794372244fd5c646f71296Chad Rosier
42ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  // If not DWORD aligned or size is more than the threshold, call the library.
43ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  // The libc version is likely to be faster for these cases. It can use the
44ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  // address value and run time information about the CPU.
45ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  if ((Align & 3) != 0 ||
46ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      !ConstantSize ||
47ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      ConstantSize->getZExtValue() >
48ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman        Subtarget->getMaxInlineSizeThreshold()) {
49ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    SDValue InFlag(0, 0);
50ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
51ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    // Check to see if there is a specialized entry-point for memory zeroing.
52ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
53ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
54ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    if (const char *bzeroEntry =  V &&
55ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman        V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
56ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      EVT IntPtr = TLI.getPointerTy();
57ece6c6bb6329748b92403c06ac87f45c43485911Chandler Carruth      Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
58ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      TargetLowering::ArgListTy Args;
59ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      TargetLowering::ArgListEntry Entry;
60ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Entry.Node = Dst;
61ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Entry.Ty = IntPtrTy;
62ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Args.push_back(Entry);
63ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Entry.Node = Size;
64ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Args.push_back(Entry);
65d2ea0e10cbd158c93fb870cdd03001b9cd1156b8Justin Holewinski      TargetLowering::
66d2ea0e10cbd158c93fb870cdd03001b9cd1156b8Justin Holewinski      CallLoweringInfo CLI(Chain, Type::getVoidTy(*DAG.getContext()),
67ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                        false, false, false, false,
684bfcd4acbc7d12aa55f8de9af84a38422f0f6d83Evan Cheng                        0, CallingConv::C, /*isTailCall=*/false,
694bfcd4acbc7d12aa55f8de9af84a38422f0f6d83Evan Cheng                        /*doesNotRet=*/false, /*isReturnValueUsed=*/false,
70ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                        DAG.getExternalSymbol(bzeroEntry, IntPtr), Args,
71ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                        DAG, dl);
72d2ea0e10cbd158c93fb870cdd03001b9cd1156b8Justin Holewinski      std::pair<SDValue,SDValue> CallResult =
73d2ea0e10cbd158c93fb870cdd03001b9cd1156b8Justin Holewinski        TLI.LowerCallTo(CLI);
74ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      return CallResult.second;
75ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    }
76ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
77ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    // Otherwise have the target-independent code call memset.
78ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    return SDValue();
79ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  }
80ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
81ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  uint64_t SizeVal = ConstantSize->getZExtValue();
82ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SDValue InFlag(0, 0);
83ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  EVT AVT;
84ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SDValue Count;
85ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
86ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  unsigned BytesLeft = 0;
87ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  bool TwoRepStos = false;
88ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  if (ValC) {
89ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    unsigned ValReg;
90ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    uint64_t Val = ValC->getZExtValue() & 255;
91ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
92ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    // If the value is a constant, then we can potentially use larger sets.
93ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    switch (Align & 3) {
94ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    case 2:   // WORD aligned
95ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      AVT = MVT::i16;
96ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      ValReg = X86::AX;
97ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Val = (Val << 8) | Val;
98ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      break;
99ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    case 0:  // DWORD aligned
100ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      AVT = MVT::i32;
101ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      ValReg = X86::EAX;
102ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Val = (Val << 8)  | Val;
103ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Val = (Val << 16) | Val;
104ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) {  // QWORD aligned
105ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman        AVT = MVT::i64;
106ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman        ValReg = X86::RAX;
107ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman        Val = (Val << 32) | Val;
108ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      }
109ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      break;
110ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    default:  // Byte aligned
111ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      AVT = MVT::i8;
112ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      ValReg = X86::AL;
113ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Count = DAG.getIntPtrConstant(SizeVal);
114ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      break;
115ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    }
116ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
117ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    if (AVT.bitsGT(MVT::i8)) {
118ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      unsigned UBytes = AVT.getSizeInBits() / 8;
119ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      Count = DAG.getIntPtrConstant(SizeVal / UBytes);
120ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman      BytesLeft = SizeVal % UBytes;
121ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    }
122ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
123ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Chain  = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
124ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                              InFlag);
125ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    InFlag = Chain.getValue(1);
126ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  } else {
127ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    AVT = MVT::i8;
128ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Count  = DAG.getIntPtrConstant(SizeVal);
129ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Chain  = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
130ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    InFlag = Chain.getValue(1);
131ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  }
132ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
133ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Chain  = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
134ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                              X86::ECX,
135ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                            Count, InFlag);
136ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  InFlag = Chain.getValue(1);
137ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Chain  = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
138ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                              X86::EDI,
139ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                            Dst, InFlag);
140ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  InFlag = Chain.getValue(1);
141ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
142f1b4eafbfec976f939ec0ea3e8acf91cef5363e3Chris Lattner  SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
143ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
144ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
145ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
146ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  if (TwoRepStos) {
147ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    InFlag = Chain.getValue(1);
148ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Count  = Size;
149ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    EVT CVT = Count.getValueType();
150ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
151ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                               DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
152ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Chain  = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
153ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                             X86::ECX,
154ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                              Left, InFlag);
155ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    InFlag = Chain.getValue(1);
156f1b4eafbfec976f939ec0ea3e8acf91cef5363e3Chris Lattner    Tys = DAG.getVTList(MVT::Other, MVT::Glue);
157ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
158ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
159ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  } else if (BytesLeft) {
160ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    // Handle the last 1 - 7 bytes.
161ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    unsigned Offset = SizeVal - BytesLeft;
162ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    EVT AddrVT = Dst.getValueType();
163ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    EVT SizeVT = Size.getValueType();
164ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
165ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Chain = DAG.getMemset(Chain, dl,
166ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                          DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
167ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                      DAG.getConstant(Offset, AddrVT)),
168ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                          Src,
169ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                          DAG.getConstant(BytesLeft, SizeVT),
170e72f2027e9116c55a5b39ac72732df8d6c45d37cChris Lattner                          Align, isVolatile, DstPtrInfo.getWithOffset(Offset));
171ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  }
172ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
173ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
174ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  return Chain;
175ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman}
176ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
177ff7a562751604a9fe13efc75bd59622244b54d35Dan GohmanSDValue
178ff7a562751604a9fe13efc75bd59622244b54d35Dan GohmanX86SelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
179ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                        SDValue Chain, SDValue Dst, SDValue Src,
180ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                        SDValue Size, unsigned Align,
181ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                        bool isVolatile, bool AlwaysInline,
182e72f2027e9116c55a5b39ac72732df8d6c45d37cChris Lattner                                         MachinePointerInfo DstPtrInfo,
183e72f2027e9116c55a5b39ac72732df8d6c45d37cChris Lattner                                         MachinePointerInfo SrcPtrInfo) const {
1847a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner  // This requires the copy size to be a constant, preferably
185ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  // within a subtarget-specific limit.
186ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
187ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  if (!ConstantSize)
188ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    return SDValue();
189ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  uint64_t SizeVal = ConstantSize->getZExtValue();
190ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  if (!AlwaysInline && SizeVal > Subtarget->getMaxInlineSizeThreshold())
191ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    return SDValue();
192ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
1931e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  /// If not DWORD aligned, it is more efficient to call the library.  However
1941e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  /// if calling the library is not allowed (AlwaysInline), then soldier on as
1951e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  /// the code generated here is better than the long load-store sequence we
1961e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  /// would otherwise get.
1971e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  if (!AlwaysInline && (Align & 3) != 0)
198ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    return SDValue();
199ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
200e54b482d1c6ec714a73d8a87a4f7583aa86fac9fChris Lattner  // If to a segment-relative address space, use the default lowering.
201e54b482d1c6ec714a73d8a87a4f7583aa86fac9fChris Lattner  if (DstPtrInfo.getAddrSpace() >= 256 ||
202e54b482d1c6ec714a73d8a87a4f7583aa86fac9fChris Lattner      SrcPtrInfo.getAddrSpace() >= 256)
203e54b482d1c6ec714a73d8a87a4f7583aa86fac9fChris Lattner    return SDValue();
2041e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands
205f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer  // ESI might be used as a base pointer, in that case we can't simply overwrite
206f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer  // the register.  Fall back to generic code.
207f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer  const X86RegisterInfo *TRI =
208f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer      static_cast<const X86RegisterInfo *>(DAG.getTarget().getRegisterInfo());
209f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer  if (TRI->hasBasePointer(DAG.getMachineFunction()) &&
210f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer      TRI->getBaseRegister() == X86::ESI)
211f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer    return SDValue();
212f09e02f01a817f4daf95ef8d3f1f2545297d32e7Benjamin Kramer
2131e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  MVT AVT;
2141e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  if (Align & 1)
2151e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands    AVT = MVT::i8;
2161e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  else if (Align & 2)
2171e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands    AVT = MVT::i16;
2181e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  else if (Align & 4)
2191e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands    // DWORD aligned
2201e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands    AVT = MVT::i32;
2211e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands  else
2221e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands    // QWORD aligned
2231e92ec688624d17b552c88afc956f61da4948cbbDuncan Sands    AVT = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
224ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
225ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  unsigned UBytes = AVT.getSizeInBits() / 8;
226ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  unsigned CountVal = SizeVal / UBytes;
227ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SDValue Count = DAG.getIntPtrConstant(CountVal);
228ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  unsigned BytesLeft = SizeVal % UBytes;
229ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
230ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SDValue InFlag(0, 0);
231ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Chain  = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
232ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                              X86::ECX,
233ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                            Count, InFlag);
234ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  InFlag = Chain.getValue(1);
235ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Chain  = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
236ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                              X86::EDI,
237ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                            Dst, InFlag);
238ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  InFlag = Chain.getValue(1);
239ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Chain  = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
240ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                              X86::ESI,
241ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                            Src, InFlag);
242ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  InFlag = Chain.getValue(1);
243ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
244f1b4eafbfec976f939ec0ea3e8acf91cef5363e3Chris Lattner  SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
245ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
246ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
247ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                array_lengthof(Ops));
248ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
249ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  SmallVector<SDValue, 4> Results;
250ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  Results.push_back(RepMovs);
251ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  if (BytesLeft) {
252ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    // Handle the last 1 - 7 bytes.
253ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    unsigned Offset = SizeVal - BytesLeft;
254ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    EVT DstVT = Dst.getValueType();
255ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    EVT SrcVT = Src.getValueType();
256ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    EVT SizeVT = Size.getValueType();
257ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman    Results.push_back(DAG.getMemcpy(Chain, dl,
258ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                    DAG.getNode(ISD::ADD, dl, DstVT, Dst,
259ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                DAG.getConstant(Offset, DstVT)),
260ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                    DAG.getNode(ISD::ADD, dl, SrcVT, Src,
261ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                                DAG.getConstant(Offset, SrcVT)),
262ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                    DAG.getConstant(BytesLeft, SizeVT),
263ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                                    Align, isVolatile, AlwaysInline,
264e72f2027e9116c55a5b39ac72732df8d6c45d37cChris Lattner                                    DstPtrInfo.getWithOffset(Offset),
265e72f2027e9116c55a5b39ac72732df8d6c45d37cChris Lattner                                    SrcPtrInfo.getWithOffset(Offset)));
266ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  }
267ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman
268ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman  return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
269ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman                     &Results[0], Results.size());
270ff7a562751604a9fe13efc75bd59622244b54d35Dan Gohman}
271