1 //===-- X86SelectionDAGInfo.cpp - X86 SelectionDAG Info -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the X86SelectionDAGInfo class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "x86-selectiondag-info"
15 #include "X86TargetMachine.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
20 X86SelectionDAGInfo::X86SelectionDAGInfo(const X86TargetMachine &TM) :
21 TargetSelectionDAGInfo(TM),
22 Subtarget(&TM.getSubtarget<X86Subtarget>()),
23 TLI(*TM.getTargetLowering()) {
26 X86SelectionDAGInfo::~X86SelectionDAGInfo() {
30 X86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
32 SDValue Dst, SDValue Src,
33 SDValue Size, unsigned Align,
35 MachinePointerInfo DstPtrInfo) const {
36 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
38 // If to a segment-relative address space, use the default lowering.
39 if (DstPtrInfo.getAddrSpace() >= 256)
42 // If not DWORD aligned or size is more than the threshold, call the library.
43 // The libc version is likely to be faster for these cases. It can use the
44 // address value and run time information about the CPU.
45 if ((Align & 3) != 0 ||
47 ConstantSize->getZExtValue() >
48 Subtarget->getMaxInlineSizeThreshold()) {
51 // Check to see if there is a specialized entry-point for memory zeroing.
52 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
54 if (const char *bzeroEntry = V &&
55 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
56 EVT IntPtr = TLI.getPointerTy();
57 Type *IntPtrTy = getTargetData()->getIntPtrType(*DAG.getContext());
58 TargetLowering::ArgListTy Args;
59 TargetLowering::ArgListEntry Entry;
62 Args.push_back(Entry);
64 Args.push_back(Entry);
66 CallLoweringInfo CLI(Chain, Type::getVoidTy(*DAG.getContext()),
67 false, false, false, false,
68 0, CallingConv::C, /*isTailCall=*/false,
69 /*doesNotRet=*/false, /*isReturnValueUsed=*/false,
70 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args,
72 std::pair<SDValue,SDValue> CallResult =
74 return CallResult.second;
77 // Otherwise have the target-independent code call memset.
81 uint64_t SizeVal = ConstantSize->getZExtValue();
85 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
86 unsigned BytesLeft = 0;
87 bool TwoRepStos = false;
90 uint64_t Val = ValC->getZExtValue() & 255;
92 // If the value is a constant, then we can potentially use larger sets.
94 case 2: // WORD aligned
97 Val = (Val << 8) | Val;
99 case 0: // DWORD aligned
102 Val = (Val << 8) | Val;
103 Val = (Val << 16) | Val;
104 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
107 Val = (Val << 32) | Val;
110 default: // Byte aligned
113 Count = DAG.getIntPtrConstant(SizeVal);
117 if (AVT.bitsGT(MVT::i8)) {
118 unsigned UBytes = AVT.getSizeInBits() / 8;
119 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
120 BytesLeft = SizeVal % UBytes;
123 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
125 InFlag = Chain.getValue(1);
128 Count = DAG.getIntPtrConstant(SizeVal);
129 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
130 InFlag = Chain.getValue(1);
133 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
136 InFlag = Chain.getValue(1);
137 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
140 InFlag = Chain.getValue(1);
142 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
143 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
144 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
147 InFlag = Chain.getValue(1);
149 EVT CVT = Count.getValueType();
150 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
151 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
152 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
155 InFlag = Chain.getValue(1);
156 Tys = DAG.getVTList(MVT::Other, MVT::Glue);
157 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
158 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
159 } else if (BytesLeft) {
160 // Handle the last 1 - 7 bytes.
161 unsigned Offset = SizeVal - BytesLeft;
162 EVT AddrVT = Dst.getValueType();
163 EVT SizeVT = Size.getValueType();
165 Chain = DAG.getMemset(Chain, dl,
166 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
167 DAG.getConstant(Offset, AddrVT)),
169 DAG.getConstant(BytesLeft, SizeVT),
170 Align, isVolatile, DstPtrInfo.getWithOffset(Offset));
173 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
178 X86SelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
179 SDValue Chain, SDValue Dst, SDValue Src,
180 SDValue Size, unsigned Align,
181 bool isVolatile, bool AlwaysInline,
182 MachinePointerInfo DstPtrInfo,
183 MachinePointerInfo SrcPtrInfo) const {
184 // This requires the copy size to be a constant, preferably
185 // within a subtarget-specific limit.
186 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
189 uint64_t SizeVal = ConstantSize->getZExtValue();
190 if (!AlwaysInline && SizeVal > Subtarget->getMaxInlineSizeThreshold())
193 /// If not DWORD aligned, it is more efficient to call the library. However
194 /// if calling the library is not allowed (AlwaysInline), then soldier on as
195 /// the code generated here is better than the long load-store sequence we
196 /// would otherwise get.
197 if (!AlwaysInline && (Align & 3) != 0)
200 // If to a segment-relative address space, use the default lowering.
201 if (DstPtrInfo.getAddrSpace() >= 256 ||
202 SrcPtrInfo.getAddrSpace() >= 256)
215 AVT = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
217 unsigned UBytes = AVT.getSizeInBits() / 8;
218 unsigned CountVal = SizeVal / UBytes;
219 SDValue Count = DAG.getIntPtrConstant(CountVal);
220 unsigned BytesLeft = SizeVal % UBytes;
222 SDValue InFlag(0, 0);
223 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
226 InFlag = Chain.getValue(1);
227 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
230 InFlag = Chain.getValue(1);
231 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
234 InFlag = Chain.getValue(1);
236 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
237 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
238 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
239 array_lengthof(Ops));
241 SmallVector<SDValue, 4> Results;
242 Results.push_back(RepMovs);
244 // Handle the last 1 - 7 bytes.
245 unsigned Offset = SizeVal - BytesLeft;
246 EVT DstVT = Dst.getValueType();
247 EVT SrcVT = Src.getValueType();
248 EVT SizeVT = Size.getValueType();
249 Results.push_back(DAG.getMemcpy(Chain, dl,
250 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
251 DAG.getConstant(Offset, DstVT)),
252 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
253 DAG.getConstant(Offset, SrcVT)),
254 DAG.getConstant(BytesLeft, SizeVT),
255 Align, isVolatile, AlwaysInline,
256 DstPtrInfo.getWithOffset(Offset),
257 SrcPtrInfo.getWithOffset(Offset)));
260 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
261 &Results[0], Results.size());