blob: 5ca40bc0091bc741096a167bc15e512595680e0b [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
Reid Klecknerab99e242014-08-29 20:50:31 +000032bool X86SelectionDAGInfo::isBaseRegConflictPossible(
33 SelectionDAG &DAG, ArrayRef<unsigned> ClobberSet) const {
34 // We cannot use TRI->hasBasePointer() until *after* we select all basic
35 // blocks. Legalization may introduce new stack temporaries with large
36 // alignment requirements. Fall back to generic code if there are any
37 // dynamic stack adjustments (hopefully rare) and the base pointer would
38 // conflict if we had to use it.
39 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
40 if (!MFI->hasVarSizedObjects() && !MFI->hasInlineAsmWithSPAdjust())
41 return false;
42
43 const X86RegisterInfo *TRI = static_cast<const X86RegisterInfo *>(
44 DAG.getSubtarget().getRegisterInfo());
45 unsigned BaseReg = TRI->getBaseRegister();
46 for (unsigned R : ClobberSet)
47 if (BaseReg == R)
48 return true;
49 return false;
50}
51
Dan Gohmanbb919df2010-05-11 17:31:57 +000052SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +000053X86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc dl,
Dan Gohmanbb919df2010-05-11 17:31:57 +000054 SDValue Chain,
55 SDValue Dst, SDValue Src,
56 SDValue Size, unsigned Align,
57 bool isVolatile,
Chris Lattner2510de22010-09-21 05:40:29 +000058 MachinePointerInfo DstPtrInfo) const {
Dan Gohmanbb919df2010-05-11 17:31:57 +000059 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Eric Christopher05b81972015-02-02 17:38:43 +000060 const X86Subtarget &Subtarget =
61 DAG.getMachineFunction().getSubtarget<X86Subtarget>();
Dan Gohmanbb919df2010-05-11 17:31:57 +000062
Reid Klecknerab99e242014-08-29 20:50:31 +000063#ifndef NDEBUG
64 // If the base register might conflict with our physical registers, bail out.
Benjamin Kramer867bfc52015-03-07 17:41:00 +000065 const unsigned ClobberSet[] = {X86::RCX, X86::RAX, X86::RDI,
66 X86::ECX, X86::EAX, X86::EDI};
Reid Klecknerab99e242014-08-29 20:50:31 +000067 assert(!isBaseRegConflictPossible(DAG, ClobberSet));
68#endif
69
Chris Lattner4470c2b2010-09-21 05:43:34 +000070 // If to a segment-relative address space, use the default lowering.
71 if (DstPtrInfo.getAddrSpace() >= 256)
72 return SDValue();
Chad Rosier24c19d22012-08-01 18:39:17 +000073
Dan Gohmanbb919df2010-05-11 17:31:57 +000074 // If not DWORD aligned or size is more than the threshold, call the library.
75 // The libc version is likely to be faster for these cases. It can use the
76 // address value and run time information about the CPU.
Eric Christophere5add682014-06-06 23:26:43 +000077 if ((Align & 3) != 0 || !ConstantSize ||
78 ConstantSize->getZExtValue() > Subtarget.getMaxInlineSizeThreshold()) {
Dan Gohmanbb919df2010-05-11 17:31:57 +000079 // Check to see if there is a specialized entry-point for memory zeroing.
80 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
81
82 if (const char *bzeroEntry = V &&
Eric Christophere5add682014-06-06 23:26:43 +000083 V->isNullValue() ? Subtarget.getBZeroEntry() : nullptr) {
84 EVT IntPtr = DAG.getTargetLoweringInfo().getPointerTy();
Chandler Carruth7ec50852012-11-01 08:07:29 +000085 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext());
Dan Gohmanbb919df2010-05-11 17:31:57 +000086 TargetLowering::ArgListTy Args;
87 TargetLowering::ArgListEntry Entry;
88 Entry.Node = Dst;
89 Entry.Ty = IntPtrTy;
90 Args.push_back(Entry);
91 Entry.Node = Size;
92 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000093
94 TargetLowering::CallLoweringInfo CLI(DAG);
95 CLI.setDebugLoc(dl).setChain(Chain)
96 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000097 DAG.getExternalSymbol(bzeroEntry, IntPtr), std::move(Args),
98 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000099 .setDiscardResult();
100
Eric Christophere5add682014-06-06 23:26:43 +0000101 std::pair<SDValue,SDValue> CallResult = DAG.getTargetLoweringInfo().LowerCallTo(CLI);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000102 return CallResult.second;
103 }
104
105 // Otherwise have the target-independent code call memset.
106 return SDValue();
107 }
108
109 uint64_t SizeVal = ConstantSize->getZExtValue();
Craig Topper062a2ba2014-04-25 05:30:21 +0000110 SDValue InFlag;
Dan Gohmanbb919df2010-05-11 17:31:57 +0000111 EVT AVT;
112 SDValue Count;
113 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
114 unsigned BytesLeft = 0;
115 bool TwoRepStos = false;
116 if (ValC) {
117 unsigned ValReg;
118 uint64_t Val = ValC->getZExtValue() & 255;
119
120 // If the value is a constant, then we can potentially use larger sets.
121 switch (Align & 3) {
122 case 2: // WORD aligned
123 AVT = MVT::i16;
124 ValReg = X86::AX;
125 Val = (Val << 8) | Val;
126 break;
127 case 0: // DWORD aligned
128 AVT = MVT::i32;
129 ValReg = X86::EAX;
130 Val = (Val << 8) | Val;
131 Val = (Val << 16) | Val;
Eric Christophere5add682014-06-06 23:26:43 +0000132 if (Subtarget.is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanbb919df2010-05-11 17:31:57 +0000133 AVT = MVT::i64;
134 ValReg = X86::RAX;
135 Val = (Val << 32) | Val;
136 }
137 break;
138 default: // Byte aligned
139 AVT = MVT::i8;
140 ValReg = X86::AL;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000141 Count = DAG.getIntPtrConstant(SizeVal, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000142 break;
143 }
144
145 if (AVT.bitsGT(MVT::i8)) {
146 unsigned UBytes = AVT.getSizeInBits() / 8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000147 Count = DAG.getIntPtrConstant(SizeVal / UBytes, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000148 BytesLeft = SizeVal % UBytes;
149 }
150
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000151 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, dl, AVT),
Dan Gohmanbb919df2010-05-11 17:31:57 +0000152 InFlag);
153 InFlag = Chain.getValue(1);
154 } else {
155 AVT = MVT::i8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000156 Count = DAG.getIntPtrConstant(SizeVal, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000157 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
158 InFlag = Chain.getValue(1);
159 }
160
Eric Christophere5add682014-06-06 23:26:43 +0000161 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX,
162 Count, InFlag);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000163 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000164 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI,
165 Dst, InFlag);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000166 InFlag = Chain.getValue(1);
167
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000168 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000169 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000170 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000171
172 if (TwoRepStos) {
173 InFlag = Chain.getValue(1);
174 Count = Size;
175 EVT CVT = Count.getValueType();
176 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000177 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, dl,
178 CVT));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000179 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
180 X86::ECX,
181 Left, InFlag);
182 InFlag = Chain.getValue(1);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000183 Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000184 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000185 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000186 } else if (BytesLeft) {
187 // Handle the last 1 - 7 bytes.
188 unsigned Offset = SizeVal - BytesLeft;
189 EVT AddrVT = Dst.getValueType();
190 EVT SizeVT = Size.getValueType();
191
192 Chain = DAG.getMemset(Chain, dl,
193 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000194 DAG.getConstant(Offset, dl, AddrVT)),
Dan Gohmanbb919df2010-05-11 17:31:57 +0000195 Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000196 DAG.getConstant(BytesLeft, dl, SizeVT),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000197 Align, isVolatile, false,
198 DstPtrInfo.getWithOffset(Offset));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000199 }
200
201 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
202 return Chain;
203}
204
Eric Christopher05b81972015-02-02 17:38:43 +0000205SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
206 SelectionDAG &DAG, SDLoc dl, SDValue Chain, SDValue Dst, SDValue Src,
207 SDValue Size, unsigned Align, bool isVolatile, bool AlwaysInline,
208 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000209 // This requires the copy size to be a constant, preferably
Dan Gohmanbb919df2010-05-11 17:31:57 +0000210 // within a subtarget-specific limit.
211 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Eric Christopher05b81972015-02-02 17:38:43 +0000212 const X86Subtarget &Subtarget =
213 DAG.getMachineFunction().getSubtarget<X86Subtarget>();
Dan Gohmanbb919df2010-05-11 17:31:57 +0000214 if (!ConstantSize)
215 return SDValue();
216 uint64_t SizeVal = ConstantSize->getZExtValue();
Eric Christophere5add682014-06-06 23:26:43 +0000217 if (!AlwaysInline && SizeVal > Subtarget.getMaxInlineSizeThreshold())
Dan Gohmanbb919df2010-05-11 17:31:57 +0000218 return SDValue();
219
Duncan Sands98512312010-11-04 21:16:46 +0000220 /// If not DWORD aligned, it is more efficient to call the library. However
221 /// if calling the library is not allowed (AlwaysInline), then soldier on as
222 /// the code generated here is better than the long load-store sequence we
223 /// would otherwise get.
224 if (!AlwaysInline && (Align & 3) != 0)
Dan Gohmanbb919df2010-05-11 17:31:57 +0000225 return SDValue();
226
Chris Lattner4470c2b2010-09-21 05:43:34 +0000227 // If to a segment-relative address space, use the default lowering.
228 if (DstPtrInfo.getAddrSpace() >= 256 ||
229 SrcPtrInfo.getAddrSpace() >= 256)
230 return SDValue();
Duncan Sands98512312010-11-04 21:16:46 +0000231
Reid Klecknerab99e242014-08-29 20:50:31 +0000232 // If the base register might conflict with our physical registers, bail out.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000233 const unsigned ClobberSet[] = {X86::RCX, X86::RSI, X86::RDI,
234 X86::ECX, X86::ESI, X86::EDI};
Reid Klecknerab99e242014-08-29 20:50:31 +0000235 if (isBaseRegConflictPossible(DAG, ClobberSet))
Hans Wennborgd683a222014-03-26 16:30:54 +0000236 return SDValue();
Benjamin Kramer8e2637e2013-02-13 13:40:35 +0000237
Duncan Sands98512312010-11-04 21:16:46 +0000238 MVT AVT;
239 if (Align & 1)
240 AVT = MVT::i8;
241 else if (Align & 2)
242 AVT = MVT::i16;
243 else if (Align & 4)
244 // DWORD aligned
245 AVT = MVT::i32;
246 else
247 // QWORD aligned
Eric Christophere5add682014-06-06 23:26:43 +0000248 AVT = Subtarget.is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanbb919df2010-05-11 17:31:57 +0000249
250 unsigned UBytes = AVT.getSizeInBits() / 8;
251 unsigned CountVal = SizeVal / UBytes;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000252 SDValue Count = DAG.getIntPtrConstant(CountVal, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000253 unsigned BytesLeft = SizeVal % UBytes;
254
Craig Topper062a2ba2014-04-25 05:30:21 +0000255 SDValue InFlag;
Eric Christophere5add682014-06-06 23:26:43 +0000256 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000257 X86::ECX,
Hans Wennborgd683a222014-03-26 16:30:54 +0000258 Count, InFlag);
259 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000260 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000261 X86::EDI,
Hans Wennborgd683a222014-03-26 16:30:54 +0000262 Dst, InFlag);
263 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000264 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RSI :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000265 X86::ESI,
Hans Wennborgd683a222014-03-26 16:30:54 +0000266 Src, InFlag);
267 InFlag = Chain.getValue(1);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000268
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000269 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Hans Wennborgd683a222014-03-26 16:30:54 +0000270 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000271 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000272
273 SmallVector<SDValue, 4> Results;
274 Results.push_back(RepMovs);
275 if (BytesLeft) {
276 // Handle the last 1 - 7 bytes.
277 unsigned Offset = SizeVal - BytesLeft;
278 EVT DstVT = Dst.getValueType();
279 EVT SrcVT = Src.getValueType();
280 EVT SizeVT = Size.getValueType();
281 Results.push_back(DAG.getMemcpy(Chain, dl,
282 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000283 DAG.getConstant(Offset, dl,
284 DstVT)),
Dan Gohmanbb919df2010-05-11 17:31:57 +0000285 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000286 DAG.getConstant(Offset, dl,
287 SrcVT)),
288 DAG.getConstant(BytesLeft, dl, SizeVT),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000289 Align, isVolatile, AlwaysInline, false,
Chris Lattner2510de22010-09-21 05:40:29 +0000290 DstPtrInfo.getWithOffset(Offset),
291 SrcPtrInfo.getWithOffset(Offset)));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000292 }
293
Craig Topper48d114b2014-04-26 18:35:24 +0000294 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Results);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000295}