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 #include "X86InstrInfo.h"
15 #include "X86ISelLowering.h"
16 #include "X86RegisterInfo.h"
17 #include "X86Subtarget.h"
18 #include "X86SelectionDAGInfo.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/Target/TargetLowering.h"
25 #define DEBUG_TYPE "x86-selectiondag-info"
27 bool X86SelectionDAGInfo::isBaseRegConflictPossible(
28 SelectionDAG &DAG, ArrayRef<MCPhysReg> ClobberSet) const {
29 // We cannot use TRI->hasBasePointer() until *after* we select all basic
30 // blocks. Legalization may introduce new stack temporaries with large
31 // alignment requirements. Fall back to generic code if there are any
32 // dynamic stack adjustments (hopefully rare) and the base pointer would
33 // conflict if we had to use it.
34 MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
35 if (!MFI.hasVarSizedObjects() && !MFI.hasOpaqueSPAdjustment())
38 const X86RegisterInfo *TRI = static_cast<const X86RegisterInfo *>(
39 DAG.getSubtarget().getRegisterInfo());
40 unsigned BaseReg = TRI->getBaseRegister();
41 for (unsigned R : ClobberSet)
47 SDValue X86SelectionDAGInfo::EmitTargetCodeForMemset(
48 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
49 SDValue Size, unsigned Align, bool isVolatile,
50 MachinePointerInfo DstPtrInfo) const {
51 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
52 const X86Subtarget &Subtarget =
53 DAG.getMachineFunction().getSubtarget<X86Subtarget>();
56 // If the base register might conflict with our physical registers, bail out.
57 const MCPhysReg ClobberSet[] = {X86::RCX, X86::RAX, X86::RDI,
58 X86::ECX, X86::EAX, X86::EDI};
59 assert(!isBaseRegConflictPossible(DAG, ClobberSet));
62 // If to a segment-relative address space, use the default lowering.
63 if (DstPtrInfo.getAddrSpace() >= 256)
66 // If not DWORD aligned or size is more than the threshold, call the library.
67 // The libc version is likely to be faster for these cases. It can use the
68 // address value and run time information about the CPU.
69 if ((Align & 3) != 0 || !ConstantSize ||
70 ConstantSize->getZExtValue() > Subtarget.getMaxInlineSizeThreshold()) {
71 // Check to see if there is a specialized entry-point for memory zeroing.
72 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
74 if (const char *bzeroEntry = V &&
75 V->isNullValue() ? Subtarget.getBZeroEntry() : nullptr) {
76 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
77 EVT IntPtr = TLI.getPointerTy(DAG.getDataLayout());
78 Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext());
79 TargetLowering::ArgListTy Args;
80 TargetLowering::ArgListEntry Entry;
83 Args.push_back(Entry);
85 Args.push_back(Entry);
87 TargetLowering::CallLoweringInfo CLI(DAG);
90 .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
91 DAG.getExternalSymbol(bzeroEntry, IntPtr),
95 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
96 return CallResult.second;
99 // Otherwise have the target-independent code call memset.
103 uint64_t SizeVal = ConstantSize->getZExtValue();
107 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
108 unsigned BytesLeft = 0;
109 bool TwoRepStos = false;
112 uint64_t Val = ValC->getZExtValue() & 255;
114 // If the value is a constant, then we can potentially use larger sets.
116 case 2: // WORD aligned
119 Val = (Val << 8) | Val;
121 case 0: // DWORD aligned
124 Val = (Val << 8) | Val;
125 Val = (Val << 16) | Val;
126 if (Subtarget.is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
129 Val = (Val << 32) | Val;
132 default: // Byte aligned
135 Count = DAG.getIntPtrConstant(SizeVal, dl);
139 if (AVT.bitsGT(MVT::i8)) {
140 unsigned UBytes = AVT.getSizeInBits() / 8;
141 Count = DAG.getIntPtrConstant(SizeVal / UBytes, dl);
142 BytesLeft = SizeVal % UBytes;
145 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, dl, AVT),
147 InFlag = Chain.getValue(1);
150 Count = DAG.getIntPtrConstant(SizeVal, dl);
151 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
152 InFlag = Chain.getValue(1);
155 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX,
157 InFlag = Chain.getValue(1);
158 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI,
160 InFlag = Chain.getValue(1);
162 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
163 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
164 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
167 InFlag = Chain.getValue(1);
169 EVT CVT = Count.getValueType();
170 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
171 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, dl,
173 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
175 InFlag = Chain.getValue(1);
176 Tys = DAG.getVTList(MVT::Other, MVT::Glue);
177 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
178 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
179 } else if (BytesLeft) {
180 // Handle the last 1 - 7 bytes.
181 unsigned Offset = SizeVal - BytesLeft;
182 EVT AddrVT = Dst.getValueType();
183 EVT SizeVT = Size.getValueType();
185 Chain = DAG.getMemset(Chain, dl,
186 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
187 DAG.getConstant(Offset, dl, AddrVT)),
189 DAG.getConstant(BytesLeft, dl, SizeVT),
190 Align, isVolatile, false,
191 DstPtrInfo.getWithOffset(Offset));
194 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
198 SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
199 SelectionDAG &DAG, const SDLoc &dl, SDValue Chain, SDValue Dst, SDValue Src,
200 SDValue Size, unsigned Align, bool isVolatile, bool AlwaysInline,
201 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
202 // This requires the copy size to be a constant, preferably
203 // within a subtarget-specific limit.
204 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
205 const X86Subtarget &Subtarget =
206 DAG.getMachineFunction().getSubtarget<X86Subtarget>();
209 uint64_t SizeVal = ConstantSize->getZExtValue();
210 if (!AlwaysInline && SizeVal > Subtarget.getMaxInlineSizeThreshold())
213 /// If not DWORD aligned, it is more efficient to call the library. However
214 /// if calling the library is not allowed (AlwaysInline), then soldier on as
215 /// the code generated here is better than the long load-store sequence we
216 /// would otherwise get.
217 if (!AlwaysInline && (Align & 3) != 0)
220 // If to a segment-relative address space, use the default lowering.
221 if (DstPtrInfo.getAddrSpace() >= 256 ||
222 SrcPtrInfo.getAddrSpace() >= 256)
225 // If the base register might conflict with our physical registers, bail out.
226 const MCPhysReg ClobberSet[] = {X86::RCX, X86::RSI, X86::RDI,
227 X86::ECX, X86::ESI, X86::EDI};
228 if (isBaseRegConflictPossible(DAG, ClobberSet))
241 AVT = Subtarget.is64Bit() ? MVT::i64 : MVT::i32;
243 unsigned UBytes = AVT.getSizeInBits() / 8;
244 unsigned CountVal = SizeVal / UBytes;
245 SDValue Count = DAG.getIntPtrConstant(CountVal, dl);
246 unsigned BytesLeft = SizeVal % UBytes;
249 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX,
251 InFlag = Chain.getValue(1);
252 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI,
254 InFlag = Chain.getValue(1);
255 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RSI : X86::ESI,
257 InFlag = Chain.getValue(1);
259 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
260 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
261 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops);
263 SmallVector<SDValue, 4> Results;
264 Results.push_back(RepMovs);
266 // Handle the last 1 - 7 bytes.
267 unsigned Offset = SizeVal - BytesLeft;
268 EVT DstVT = Dst.getValueType();
269 EVT SrcVT = Src.getValueType();
270 EVT SizeVT = Size.getValueType();
271 Results.push_back(DAG.getMemcpy(Chain, dl,
272 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
273 DAG.getConstant(Offset, dl,
275 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
276 DAG.getConstant(Offset, dl,
278 DAG.getConstant(BytesLeft, dl, SizeVT),
279 Align, isVolatile, AlwaysInline, false,
280 DstPtrInfo.getWithOffset(Offset),
281 SrcPtrInfo.getWithOffset(Offset)));
284 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Results);