blob: edd9a429f8b787f67bff7421f693332734e1854a [file] [log] [blame]
Dan Gohman9becddd2010-04-16 23:04:22 +00001//===-- X86SelectionDAGInfo.cpp - X86 SelectionDAG Info -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the X86SelectionDAGInfo class.
11//
12//===----------------------------------------------------------------------===//
13
Eric Christophere5add682014-06-06 23:26:43 +000014#include "X86InstrInfo.h"
15#include "X86ISelLowering.h"
16#include "X86RegisterInfo.h"
17#include "X86Subtarget.h"
18#include "X86SelectionDAGInfo.h"
Dan Gohmanbb919df2010-05-11 17:31:57 +000019#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DerivedTypes.h"
Eric Christophere5add682014-06-06 23:26:43 +000021#include "llvm/Target/TargetLowering.h"
22
Dan Gohman9becddd2010-04-16 23:04:22 +000023using namespace llvm;
24
Chandler Carruth84e68b22014-04-22 02:41:26 +000025#define DEBUG_TYPE "x86-selectiondag-info"
26
Eric Christophere5add682014-06-06 23:26:43 +000027X86SelectionDAGInfo::X86SelectionDAGInfo(const DataLayout &DL)
28 : TargetSelectionDAGInfo(&DL) {}
Dan Gohman9becddd2010-04-16 23:04:22 +000029
Eric Christophere5add682014-06-06 23:26:43 +000030X86SelectionDAGInfo::~X86SelectionDAGInfo() {}
Dan Gohmanbb919df2010-05-11 17:31:57 +000031
32SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +000033X86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc dl,
Dan Gohmanbb919df2010-05-11 17:31:57 +000034 SDValue Chain,
35 SDValue Dst, SDValue Src,
36 SDValue Size, unsigned Align,
37 bool isVolatile,
Chris Lattner2510de22010-09-21 05:40:29 +000038 MachinePointerInfo DstPtrInfo) const {
Dan Gohmanbb919df2010-05-11 17:31:57 +000039 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Eric Christophere5add682014-06-06 23:26:43 +000040 const X86Subtarget &Subtarget = DAG.getTarget().getSubtarget<X86Subtarget>();
Dan Gohmanbb919df2010-05-11 17:31:57 +000041
Chris Lattner4470c2b2010-09-21 05:43:34 +000042 // If to a segment-relative address space, use the default lowering.
43 if (DstPtrInfo.getAddrSpace() >= 256)
44 return SDValue();
Chad Rosier24c19d22012-08-01 18:39:17 +000045
Dan Gohmanbb919df2010-05-11 17:31:57 +000046 // If not DWORD aligned or size is more than the threshold, call the library.
47 // The libc version is likely to be faster for these cases. It can use the
48 // address value and run time information about the CPU.
Eric Christophere5add682014-06-06 23:26:43 +000049 if ((Align & 3) != 0 || !ConstantSize ||
50 ConstantSize->getZExtValue() > Subtarget.getMaxInlineSizeThreshold()) {
Dan Gohmanbb919df2010-05-11 17:31:57 +000051 // Check to see if there is a specialized entry-point for memory zeroing.
52 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
53
54 if (const char *bzeroEntry = V &&
Eric Christophere5add682014-06-06 23:26:43 +000055 V->isNullValue() ? Subtarget.getBZeroEntry() : nullptr) {
56 EVT IntPtr = DAG.getTargetLoweringInfo().getPointerTy();
Chandler Carruth7ec50852012-11-01 08:07:29 +000057 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
Dan Gohmanbb919df2010-05-11 17:31:57 +000058 TargetLowering::ArgListTy Args;
59 TargetLowering::ArgListEntry Entry;
60 Entry.Node = Dst;
61 Entry.Ty = IntPtrTy;
62 Args.push_back(Entry);
63 Entry.Node = Size;
64 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000065
66 TargetLowering::CallLoweringInfo CLI(DAG);
67 CLI.setDebugLoc(dl).setChain(Chain)
68 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000069 DAG.getExternalSymbol(bzeroEntry, IntPtr), std::move(Args),
70 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000071 .setDiscardResult();
72
Eric Christophere5add682014-06-06 23:26:43 +000073 std::pair<SDValue,SDValue> CallResult = DAG.getTargetLoweringInfo().LowerCallTo(CLI);
Dan Gohmanbb919df2010-05-11 17:31:57 +000074 return CallResult.second;
75 }
76
77 // Otherwise have the target-independent code call memset.
78 return SDValue();
79 }
80
81 uint64_t SizeVal = ConstantSize->getZExtValue();
Craig Topper062a2ba2014-04-25 05:30:21 +000082 SDValue InFlag;
Dan Gohmanbb919df2010-05-11 17:31:57 +000083 EVT AVT;
84 SDValue Count;
85 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
86 unsigned BytesLeft = 0;
87 bool TwoRepStos = false;
88 if (ValC) {
89 unsigned ValReg;
90 uint64_t Val = ValC->getZExtValue() & 255;
91
92 // If the value is a constant, then we can potentially use larger sets.
93 switch (Align & 3) {
94 case 2: // WORD aligned
95 AVT = MVT::i16;
96 ValReg = X86::AX;
97 Val = (Val << 8) | Val;
98 break;
99 case 0: // DWORD aligned
100 AVT = MVT::i32;
101 ValReg = X86::EAX;
102 Val = (Val << 8) | Val;
103 Val = (Val << 16) | Val;
Eric Christophere5add682014-06-06 23:26:43 +0000104 if (Subtarget.is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanbb919df2010-05-11 17:31:57 +0000105 AVT = MVT::i64;
106 ValReg = X86::RAX;
107 Val = (Val << 32) | Val;
108 }
109 break;
110 default: // Byte aligned
111 AVT = MVT::i8;
112 ValReg = X86::AL;
113 Count = DAG.getIntPtrConstant(SizeVal);
114 break;
115 }
116
117 if (AVT.bitsGT(MVT::i8)) {
118 unsigned UBytes = AVT.getSizeInBits() / 8;
119 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
120 BytesLeft = SizeVal % UBytes;
121 }
122
123 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
124 InFlag);
125 InFlag = Chain.getValue(1);
126 } else {
127 AVT = MVT::i8;
128 Count = DAG.getIntPtrConstant(SizeVal);
129 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
130 InFlag = Chain.getValue(1);
131 }
132
Eric Christophere5add682014-06-06 23:26:43 +0000133 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX,
134 Count, InFlag);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000135 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000136 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI,
137 Dst, InFlag);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000138 InFlag = Chain.getValue(1);
139
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000140 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000141 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000142 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000143
144 if (TwoRepStos) {
145 InFlag = Chain.getValue(1);
146 Count = Size;
147 EVT CVT = Count.getValueType();
148 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
149 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
150 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
151 X86::ECX,
152 Left, InFlag);
153 InFlag = Chain.getValue(1);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000154 Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000155 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000156 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000157 } else if (BytesLeft) {
158 // Handle the last 1 - 7 bytes.
159 unsigned Offset = SizeVal - BytesLeft;
160 EVT AddrVT = Dst.getValueType();
161 EVT SizeVT = Size.getValueType();
162
163 Chain = DAG.getMemset(Chain, dl,
164 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
165 DAG.getConstant(Offset, AddrVT)),
166 Src,
167 DAG.getConstant(BytesLeft, SizeVT),
Chris Lattner2510de22010-09-21 05:40:29 +0000168 Align, isVolatile, DstPtrInfo.getWithOffset(Offset));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000169 }
170
171 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
172 return Chain;
173}
174
175SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +0000176X86SelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, SDLoc dl,
Dan Gohmanbb919df2010-05-11 17:31:57 +0000177 SDValue Chain, SDValue Dst, SDValue Src,
178 SDValue Size, unsigned Align,
179 bool isVolatile, bool AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +0000180 MachinePointerInfo DstPtrInfo,
181 MachinePointerInfo SrcPtrInfo) const {
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000182 // This requires the copy size to be a constant, preferably
Dan Gohmanbb919df2010-05-11 17:31:57 +0000183 // within a subtarget-specific limit.
184 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Eric Christophere5add682014-06-06 23:26:43 +0000185 const X86Subtarget &Subtarget = DAG.getTarget().getSubtarget<X86Subtarget>();
Dan Gohmanbb919df2010-05-11 17:31:57 +0000186 if (!ConstantSize)
187 return SDValue();
188 uint64_t SizeVal = ConstantSize->getZExtValue();
Eric Christophere5add682014-06-06 23:26:43 +0000189 if (!AlwaysInline && SizeVal > Subtarget.getMaxInlineSizeThreshold())
Dan Gohmanbb919df2010-05-11 17:31:57 +0000190 return SDValue();
191
Duncan Sands98512312010-11-04 21:16:46 +0000192 /// If not DWORD aligned, it is more efficient to call the library. However
193 /// if calling the library is not allowed (AlwaysInline), then soldier on as
194 /// the code generated here is better than the long load-store sequence we
195 /// would otherwise get.
196 if (!AlwaysInline && (Align & 3) != 0)
Dan Gohmanbb919df2010-05-11 17:31:57 +0000197 return SDValue();
198
Chris Lattner4470c2b2010-09-21 05:43:34 +0000199 // If to a segment-relative address space, use the default lowering.
200 if (DstPtrInfo.getAddrSpace() >= 256 ||
201 SrcPtrInfo.getAddrSpace() >= 256)
202 return SDValue();
Duncan Sands98512312010-11-04 21:16:46 +0000203
Hans Wennborgd683a222014-03-26 16:30:54 +0000204 // ESI might be used as a base pointer, in that case we can't simply overwrite
205 // the register. Fall back to generic code.
Eric Christopherd9134482014-08-04 21:25:23 +0000206 const X86RegisterInfo *TRI = static_cast<const X86RegisterInfo *>(
Eric Christopherfc6de422014-08-05 02:39:49 +0000207 DAG.getSubtarget().getRegisterInfo());
Hans Wennborgd683a222014-03-26 16:30:54 +0000208 if (TRI->hasBasePointer(DAG.getMachineFunction()) &&
209 TRI->getBaseRegister() == X86::ESI)
210 return SDValue();
Benjamin Kramer8e2637e2013-02-13 13:40:35 +0000211
Duncan Sands98512312010-11-04 21:16:46 +0000212 MVT AVT;
213 if (Align & 1)
214 AVT = MVT::i8;
215 else if (Align & 2)
216 AVT = MVT::i16;
217 else if (Align & 4)
218 // DWORD aligned
219 AVT = MVT::i32;
220 else
221 // QWORD aligned
Eric Christophere5add682014-06-06 23:26:43 +0000222 AVT = Subtarget.is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanbb919df2010-05-11 17:31:57 +0000223
224 unsigned UBytes = AVT.getSizeInBits() / 8;
225 unsigned CountVal = SizeVal / UBytes;
226 SDValue Count = DAG.getIntPtrConstant(CountVal);
227 unsigned BytesLeft = SizeVal % UBytes;
228
Craig Topper062a2ba2014-04-25 05:30:21 +0000229 SDValue InFlag;
Eric Christophere5add682014-06-06 23:26:43 +0000230 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000231 X86::ECX,
Hans Wennborgd683a222014-03-26 16:30:54 +0000232 Count, InFlag);
233 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000234 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000235 X86::EDI,
Hans Wennborgd683a222014-03-26 16:30:54 +0000236 Dst, InFlag);
237 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000238 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RSI :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000239 X86::ESI,
Hans Wennborgd683a222014-03-26 16:30:54 +0000240 Src, InFlag);
241 InFlag = Chain.getValue(1);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000242
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000243 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Hans Wennborgd683a222014-03-26 16:30:54 +0000244 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000245 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000246
247 SmallVector<SDValue, 4> Results;
248 Results.push_back(RepMovs);
249 if (BytesLeft) {
250 // Handle the last 1 - 7 bytes.
251 unsigned Offset = SizeVal - BytesLeft;
252 EVT DstVT = Dst.getValueType();
253 EVT SrcVT = Src.getValueType();
254 EVT SizeVT = Size.getValueType();
255 Results.push_back(DAG.getMemcpy(Chain, dl,
256 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
257 DAG.getConstant(Offset, DstVT)),
258 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
259 DAG.getConstant(Offset, SrcVT)),
260 DAG.getConstant(BytesLeft, SizeVT),
261 Align, isVolatile, AlwaysInline,
Chris Lattner2510de22010-09-21 05:40:29 +0000262 DstPtrInfo.getWithOffset(Offset),
263 SrcPtrInfo.getWithOffset(Offset)));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000264 }
265
Craig Topper48d114b2014-04-26 18:35:24 +0000266 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Results);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000267}